ClientMapper.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\OAuth2\Db;
  8. use OCA\OAuth2\Exceptions\ClientNotFoundException;
  9. use OCP\AppFramework\Db\IMapperException;
  10. use OCP\AppFramework\Db\QBMapper;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\IDBConnection;
  13. /**
  14. * @template-extends QBMapper<Client>
  15. */
  16. class ClientMapper extends QBMapper {
  17. /**
  18. * @param IDBConnection $db
  19. */
  20. public function __construct(IDBConnection $db) {
  21. parent::__construct($db, 'oauth2_clients');
  22. }
  23. /**
  24. * @param string $clientIdentifier
  25. * @return Client
  26. * @throws ClientNotFoundException
  27. */
  28. public function getByIdentifier(string $clientIdentifier): Client {
  29. $qb = $this->db->getQueryBuilder();
  30. $qb
  31. ->select('*')
  32. ->from($this->tableName)
  33. ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier)));
  34. try {
  35. $client = $this->findEntity($qb);
  36. } catch (IMapperException $e) {
  37. throw new ClientNotFoundException('could not find client ' . $clientIdentifier, 0, $e);
  38. }
  39. return $client;
  40. }
  41. /**
  42. * @param int $id internal id of the client
  43. * @return Client
  44. * @throws ClientNotFoundException
  45. */
  46. public function getByUid(int $id): Client {
  47. $qb = $this->db->getQueryBuilder();
  48. $qb
  49. ->select('*')
  50. ->from($this->tableName)
  51. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  52. try {
  53. $client = $this->findEntity($qb);
  54. } catch (IMapperException $e) {
  55. throw new ClientNotFoundException('could not find client with id ' . $id, 0, $e);
  56. }
  57. return $client;
  58. }
  59. /**
  60. * @return Client[]
  61. */
  62. public function getClients(): array {
  63. $qb = $this->db->getQueryBuilder();
  64. $qb
  65. ->select('*')
  66. ->from($this->tableName);
  67. return $this->findEntities($qb);
  68. }
  69. }