Passed
Push — master ( 902adb...3cf321 )
by Christoph
10:44 queued 11s
created

CardSearchDao::findExisting()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 34
c 1
b 0
f 0
nc 16
nop 4
dl 0
loc 49
rs 9.0648
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
32
class CardSearchDao {
33
34
	/** @var IDBConnection */
35
	private $db;
36
37
	public function __construct(IDBConnection $db) {
38
		$this->db = $db;
39
	}
40
41
	public function findExisting(IUser $user,
42
								 ?string $uid,
43
								 ?string $email,
44
								 ?string $cloudId): ?string {
45
		$addressbooksQuery = $this->db->getQueryBuilder();
46
		$cardQuery = $this->db->getQueryBuilder();
47
		$propQuery = $this->db->getQueryBuilder();
48
49
		$propOr = $propQuery->expr()->orX();
50
		if ($uid !== null) {
51
			$propOr->add($propQuery->expr()->andX(
52
				$propQuery->expr()->eq('name', $cardQuery->createNamedParameter('UID')),
53
				$propQuery->expr()->eq('value', $cardQuery->createNamedParameter($uid))
54
			));
55
		}
56
		if ($email !== null) {
57
			$propOr->add($propQuery->expr()->andX(
58
				$propQuery->expr()->eq('name', $cardQuery->createNamedParameter('EMAIL')),
59
				$propQuery->expr()->eq('value', $cardQuery->createNamedParameter($email))
60
			));
61
		}
62
		if ($cloudId !== null) {
63
			$propOr->add($propQuery->expr()->andX(
64
				$propQuery->expr()->eq('name', $cardQuery->createNamedParameter('CLOUD')),
65
				$propQuery->expr()->eq('value', $cardQuery->createNamedParameter($cloudId))
66
			));
67
		}
68
		$addressbooksQuery->selectDistinct('id')
69
			->from('addressbooks')
70
			->where($addressbooksQuery->expr()->eq('principaluri', $cardQuery->createNamedParameter("principals/users/" . $user->getUID())));
71
		$propQuery->selectDistinct('cardid')
72
			->from('cards_properties')
73
			->where($propQuery->expr()->in('addressbookid', $propQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
74
			->andWhere($propOr)
75
			->groupBy('cardid');
76
		$cardQuery->select('carddata')
77
			->from('cards')
78
			->where($cardQuery->expr()->in('id', $cardQuery->createFunction($propQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
79
			->andWhere($cardQuery->expr()->in('addressbookid', $cardQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
80
			->setMaxResults(1);
81
		$result = $cardQuery->execute();
82
		/** @var string|false $card */
83
		$card = $result->fetchColumn(0);
84
85
		if ($card === false) {
0 ignored issues
show
introduced by
The condition $card === false is always true.
Loading history...
86
			return null;
87
		}
88
89
		return $card;
90
	}
91
92
}
93