KnownUserMapper.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\KnownUser;
  25. use OCP\AppFramework\Db\QBMapper;
  26. use OCP\IDBConnection;
  27. /**
  28. * @method KnownUser mapRowToEntity(array $row)
  29. *
  30. * @template-extends QBMapper<KnownUser>
  31. */
  32. class KnownUserMapper extends QBMapper {
  33. /**
  34. * @param IDBConnection $db
  35. */
  36. public function __construct(IDBConnection $db) {
  37. parent::__construct($db, 'known_users', KnownUser::class);
  38. }
  39. /**
  40. * @param string $knownTo
  41. * @return int Number of deleted entities
  42. */
  43. public function deleteKnownTo(string $knownTo): int {
  44. $query = $this->db->getQueryBuilder();
  45. $query->delete($this->getTableName())
  46. ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
  47. return $query->executeStatement();
  48. }
  49. /**
  50. * @param string $knownUser
  51. * @return int Number of deleted entities
  52. */
  53. public function deleteKnownUser(string $knownUser): int {
  54. $query = $this->db->getQueryBuilder();
  55. $query->delete($this->getTableName())
  56. ->where($query->expr()->eq('known_user', $query->createNamedParameter($knownUser)));
  57. return $query->executeStatement();
  58. }
  59. /**
  60. * Returns all "known users" for the given "known to" user
  61. *
  62. * @param string $knownTo
  63. * @return KnownUser[]
  64. */
  65. public function getKnownUsers(string $knownTo): array {
  66. $query = $this->db->getQueryBuilder();
  67. $query->select('*')
  68. ->from($this->getTableName())
  69. ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
  70. return $this->findEntities($query);
  71. }
  72. public function createKnownUserFromRow(array $row): KnownUser {
  73. return $this->mapRowToEntity([
  74. 'id' => $row['s_id'],
  75. 'known_to' => $row['known_to'],
  76. 'known_user' => $row['known_user'],
  77. ]);
  78. }
  79. }