KnownUserMapper.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. class KnownUserMapper extends QBMapper {
  31. /**
  32. * @param IDBConnection $db
  33. */
  34. public function __construct(IDBConnection $db) {
  35. parent::__construct($db, 'known_users', KnownUser::class);
  36. }
  37. /**
  38. * @param string $knownTo
  39. * @return int Number of deleted entities
  40. */
  41. public function deleteKnownTo(string $knownTo): int {
  42. $query = $this->db->getQueryBuilder();
  43. $query->delete($this->getTableName())
  44. ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
  45. return $query->executeStatement();
  46. }
  47. /**
  48. * @param string $knownUser
  49. * @return int Number of deleted entities
  50. */
  51. public function deleteKnownUser(string $knownUser): int {
  52. $query = $this->db->getQueryBuilder();
  53. $query->delete($this->getTableName())
  54. ->where($query->expr()->eq('known_user', $query->createNamedParameter($knownUser)));
  55. return $query->executeStatement();
  56. }
  57. /**
  58. * Returns all "known users" for the given "known to" user
  59. *
  60. * @param string $knownTo
  61. * @return KnownUser[]
  62. */
  63. public function getKnownUsers(string $knownTo): array {
  64. $query = $this->db->getQueryBuilder();
  65. $query->select('*')
  66. ->from($this->getTableName())
  67. ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
  68. return $this->findEntities($query);
  69. }
  70. public function createKnownUserFromRow(array $row): KnownUser {
  71. return $this->mapRowToEntity([
  72. 'id' => $row['s_id'],
  73. 'known_to' => $row['known_to'],
  74. 'known_user' => $row['known_user'],
  75. ]);
  76. }
  77. }