Passed
Push — master ( fd76bf...cd270d )
by Christoph
13:45
created

CardSearchDao::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OCA\ContactsInteraction\Db;
27
28
use OCP\DB\QueryBuilder\IQueryBuilder;
29
use OCP\IDBConnection;
30
use OCP\IUser;
31
use function is_resource;
32
use function stream_get_contents;
33
34
class CardSearchDao {
35
36
	/** @var IDBConnection */
37
	private $db;
38
39
	public function __construct(IDBConnection $db) {
40
		$this->db = $db;
41
	}
42
43
	public function findExisting(IUser $user,
44
								 ?string $uid,
45
								 ?string $email,
46
								 ?string $cloudId): ?string {
47
		$addressbooksQuery = $this->db->getQueryBuilder();
48
		$cardQuery = $this->db->getQueryBuilder();
49
		$propQuery = $this->db->getQueryBuilder();
50
51
		$propOr = $propQuery->expr()->orX();
52
		if ($uid !== null) {
53
			$propOr->add($propQuery->expr()->andX(
54
				$propQuery->expr()->eq('name', $cardQuery->createNamedParameter('UID')),
55
				$propQuery->expr()->eq('value', $cardQuery->createNamedParameter($uid))
56
			));
57
		}
58
		if ($email !== null) {
59
			$propOr->add($propQuery->expr()->andX(
60
				$propQuery->expr()->eq('name', $cardQuery->createNamedParameter('EMAIL')),
61
				$propQuery->expr()->eq('value', $cardQuery->createNamedParameter($email))
62
			));
63
		}
64
		if ($cloudId !== null) {
65
			$propOr->add($propQuery->expr()->andX(
66
				$propQuery->expr()->eq('name', $cardQuery->createNamedParameter('CLOUD')),
67
				$propQuery->expr()->eq('value', $cardQuery->createNamedParameter($cloudId))
68
			));
69
		}
70
		$addressbooksQuery->selectDistinct('id')
71
			->from('addressbooks')
72
			->where($addressbooksQuery->expr()->eq('principaluri', $cardQuery->createNamedParameter("principals/users/" . $user->getUID())));
73
		$propQuery->selectDistinct('cardid')
74
			->from('cards_properties')
75
			->where($propQuery->expr()->in('addressbookid', $propQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
76
			->andWhere($propOr)
77
			->groupBy('cardid');
78
		$cardQuery->select('carddata')
79
			->from('cards')
80
			->where($cardQuery->expr()->in('id', $cardQuery->createFunction($propQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
81
			->andWhere($cardQuery->expr()->in('addressbookid', $cardQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
82
			->setMaxResults(1);
83
		$result = $cardQuery->execute();
84
		/** @var string|resource|false $card */
85
		$card = $result->fetchColumn(0);
86
87
		if ($card === false) {
0 ignored issues
show
introduced by
The condition $card === false is always true.
Loading history...
88
			return null;
89
		}
90
		if (is_resource($card)) {
91
			return stream_get_contents($card);
92
		}
93
94
		return $card;
95
	}
96
}
97