CardSearchDao.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\ContactsInteraction\Db;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\IDBConnection;
  10. use OCP\IUser;
  11. use function is_resource;
  12. use function stream_get_contents;
  13. class CardSearchDao {
  14. public function __construct(
  15. private IDBConnection $db,
  16. ) {
  17. }
  18. public function findExisting(IUser $user,
  19. ?string $uid,
  20. ?string $email,
  21. ?string $cloudId): ?string {
  22. $addressbooksQuery = $this->db->getQueryBuilder();
  23. $cardQuery = $this->db->getQueryBuilder();
  24. $propQuery = $this->db->getQueryBuilder();
  25. $additionalWheres = [];
  26. if ($uid !== null) {
  27. $additionalWheres[] = $propQuery->expr()->andX(
  28. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('UID')),
  29. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($uid))
  30. );
  31. }
  32. if ($email !== null) {
  33. $additionalWheres[] = $propQuery->expr()->andX(
  34. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('EMAIL')),
  35. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($email))
  36. );
  37. }
  38. if ($cloudId !== null) {
  39. $additionalWheres[] = $propQuery->expr()->andX(
  40. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('CLOUD')),
  41. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($cloudId))
  42. );
  43. }
  44. $addressbooksQuery->selectDistinct('id')
  45. ->from('addressbooks')
  46. ->where($addressbooksQuery->expr()->eq('principaluri', $cardQuery->createNamedParameter('principals/users/' . $user->getUID())));
  47. $propQuery->selectDistinct('cardid')
  48. ->from('cards_properties')
  49. ->where($propQuery->expr()->in('addressbookid', $propQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  50. ->groupBy('cardid');
  51. if (!empty($additionalWheres)) {
  52. $propQuery->andWhere($propQuery->expr()->orX(...$additionalWheres));
  53. }
  54. $cardQuery->select('carddata')
  55. ->from('cards')
  56. ->where($cardQuery->expr()->in('id', $cardQuery->createFunction($propQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  57. ->andWhere($cardQuery->expr()->in('addressbookid', $cardQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  58. ->setMaxResults(1);
  59. $result = $cardQuery->executeQuery();
  60. /** @var string|resource|false $card */
  61. $card = $result->fetchOne();
  62. if ($card === false) {
  63. return null;
  64. }
  65. if (is_resource($card)) {
  66. return stream_get_contents($card);
  67. }
  68. return $card;
  69. }
  70. }