1
0

KnownUserMapper.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\KnownUser;
  8. use OCP\AppFramework\Db\QBMapper;
  9. use OCP\IDBConnection;
  10. /**
  11. * @method KnownUser mapRowToEntity(array $row)
  12. *
  13. * @template-extends QBMapper<KnownUser>
  14. */
  15. class KnownUserMapper extends QBMapper {
  16. /**
  17. * @param IDBConnection $db
  18. */
  19. public function __construct(IDBConnection $db) {
  20. parent::__construct($db, 'known_users', KnownUser::class);
  21. }
  22. /**
  23. * @param string $knownTo
  24. * @return int Number of deleted entities
  25. */
  26. public function deleteKnownTo(string $knownTo): int {
  27. $query = $this->db->getQueryBuilder();
  28. $query->delete($this->getTableName())
  29. ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
  30. return $query->executeStatement();
  31. }
  32. /**
  33. * @param string $knownUser
  34. * @return int Number of deleted entities
  35. */
  36. public function deleteKnownUser(string $knownUser): int {
  37. $query = $this->db->getQueryBuilder();
  38. $query->delete($this->getTableName())
  39. ->where($query->expr()->eq('known_user', $query->createNamedParameter($knownUser)));
  40. return $query->executeStatement();
  41. }
  42. /**
  43. * Returns all "known users" for the given "known to" user
  44. *
  45. * @param string $knownTo
  46. * @return KnownUser[]
  47. */
  48. public function getKnownUsers(string $knownTo): array {
  49. $query = $this->db->getQueryBuilder();
  50. $query->select('*')
  51. ->from($this->getTableName())
  52. ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
  53. return $this->findEntities($query);
  54. }
  55. public function createKnownUserFromRow(array $row): KnownUser {
  56. return $this->mapRowToEntity([
  57. 'id' => $row['s_id'],
  58. 'known_to' => $row['known_to'],
  59. 'known_user' => $row['known_user'],
  60. ]);
  61. }
  62. }