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

RecentContactMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
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\AppFramework\Db\DoesNotExistException;
29
use OCP\AppFramework\Db\QBMapper;
30
use OCP\IDBConnection;
31
use OCP\IUser;
32
33
class RecentContactMapper extends QBMapper {
34
35
	public const TABLE_NAME = 'recent_contact';
36
37
	public function __construct(IDBConnection $db) {
38
		parent::__construct($db, self::TABLE_NAME);
39
	}
40
41
	/**
42
	 * @return RecentContact[]
43
	 */
44
	public function findAll(string $uid): array {
45
		$qb = $this->db->getQueryBuilder();
46
47
		$select = $qb
48
			->select('*')
49
			->from($this->getTableName())
50
			->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
51
52
		return $this->findEntities($select);
53
	}
54
55
	/**
56
	 * @param string $uid
57
	 * @param int $id
58
	 *
59
	 * @return RecentContact
60
	 * @throws DoesNotExistException
61
	 */
62
	public function find(string $uid, int $id): RecentContact {
63
		$qb = $this->db->getQueryBuilder();
64
65
		$select = $qb
66
			->select('*')
67
			->from($this->getTableName())
68
			->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $qb::PARAM_INT)))
69
			->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
70
71
		return $this->findEntity($select);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findEntity($select) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\ContactsInteraction\Db\RecentContact.
Loading history...
72
	}
73
74
	/**
75
	 * @param IUser $user
76
	 * @param string|null $uid
77
	 * @param string|null $email
78
	 * @param string|null $cloudId
79
	 *
80
	 * @return RecentContact[]
81
	 */
82
	public function findMatch(IUser $user,
83
							  ?string $uid,
84
							  ?string $email,
85
							  ?string $cloudId): array {
86
		$qb = $this->db->getQueryBuilder();
87
88
		$or = $qb->expr()->orX();
89
		if ($uid !== null) {
90
			$or->add($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
91
		}
92
		if ($email !== null) {
93
			$or->add($qb->expr()->eq('email', $qb->createNamedParameter($email)));
94
		}
95
		if ($cloudId !== null) {
96
			$or->add($qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId)));
97
		}
98
99
		$select = $qb
100
			->select('*')
101
			->from($this->getTableName())
102
			->where($or)
103
			->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID())));
104
105
		return $this->findEntities($select);
106
	}
107
108
	public function cleanUp(int $olderThan): void {
109
		$qb = $this->db->getQueryBuilder();
110
111
		$delete = $qb
112
			->delete($this->getTableName())
113
			->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan)));
114
115
		$delete->execute();
116
	}
117
118
}
119