ClientMapper.php 2.4 KB

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