ClientMapper.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\OAuth2\Db;
  27. use OCA\OAuth2\Exceptions\ClientNotFoundException;
  28. use OCP\AppFramework\Db\IMapperException;
  29. use OCP\AppFramework\Db\QBMapper;
  30. use OCP\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\IDBConnection;
  32. /**
  33. * @template-extends QBMapper<Client>
  34. */
  35. class ClientMapper extends QBMapper {
  36. /**
  37. * @param IDBConnection $db
  38. */
  39. public function __construct(IDBConnection $db) {
  40. parent::__construct($db, 'oauth2_clients');
  41. }
  42. /**
  43. * @param string $clientIdentifier
  44. * @return Client
  45. * @throws ClientNotFoundException
  46. */
  47. public function getByIdentifier(string $clientIdentifier): Client {
  48. $qb = $this->db->getQueryBuilder();
  49. $qb
  50. ->select('*')
  51. ->from($this->tableName)
  52. ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier)));
  53. try {
  54. $client = $this->findEntity($qb);
  55. } catch (IMapperException $e) {
  56. throw new ClientNotFoundException('could not find client '.$clientIdentifier, 0, $e);
  57. }
  58. return $client;
  59. }
  60. /**
  61. * @param int $id internal id of the client
  62. * @return Client
  63. * @throws ClientNotFoundException
  64. */
  65. public function getByUid(int $id): Client {
  66. $qb = $this->db->getQueryBuilder();
  67. $qb
  68. ->select('*')
  69. ->from($this->tableName)
  70. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  71. try {
  72. $client = $this->findEntity($qb);
  73. } catch (IMapperException $e) {
  74. throw new ClientNotFoundException('could not find client with id '.$id, 0, $e);
  75. }
  76. return $client;
  77. }
  78. /**
  79. * @return Client[]
  80. */
  81. public function getClients(): array {
  82. $qb = $this->db->getQueryBuilder();
  83. $qb
  84. ->select('*')
  85. ->from($this->tableName);
  86. return $this->findEntities($qb);
  87. }
  88. }