Passed
Push — master ( a4d511...6fbf8f )
by Morris
20:15 queued 11s
created

UserStatusMapper::findByUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\UserStatus\Db;
27
28
use OCP\AppFramework\Db\QBMapper;
29
use OCP\DB\QueryBuilder\IQueryBuilder;
30
use OCP\IDBConnection;
31
32
/**
33
 * Class UserStatusMapper
34
 *
35
 * @package OCA\UserStatus\Db
36
 *
37
 * @method UserStatus insert(UserStatus $entity)
38
 * @method UserStatus update(UserStatus $entity)
39
 * @method UserStatus insertOrUpdate(UserStatus $entity)
40
 * @method UserStatus delete(UserStatus $entity)
41
 */
42
class UserStatusMapper extends QBMapper {
43
44
	/**
45
	 * @param IDBConnection $db
46
	 */
47
	public function __construct(IDBConnection $db) {
48
		parent::__construct($db, 'user_status');
49
	}
50
51
	/**
52
	 * @param int|null $limit
53
	 * @param int|null $offset
54
	 * @return UserStatus[]
55
	 */
56
	public function findAll(?int $limit = null, ?int $offset = null):array {
57
		$qb = $this->db->getQueryBuilder();
58
		$qb
59
			->select('*')
60
			->from($this->tableName);
61
62
		if ($limit !== null) {
63
			$qb->setMaxResults($limit);
64
		}
65
		if ($offset !== null) {
66
			$qb->setFirstResult($offset);
67
		}
68
69
		return $this->findEntities($qb);
70
	}
71
72
	/**
73
	 * @param string $userId
74
	 * @return UserStatus
75
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
76
	 */
77
	public function findByUserId(string $userId):UserStatus {
78
		$qb = $this->db->getQueryBuilder();
79
		$qb
80
			->select('*')
81
			->from($this->tableName)
82
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
83
84
		return $this->findEntity($qb);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findEntity($qb) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\UserStatus\Db\UserStatus.
Loading history...
85
	}
86
87
	/**
88
	 * @param array $userIds
89
	 * @return array
90
	 */
91
	public function findByUserIds(array $userIds):array {
92
		$qb = $this->db->getQueryBuilder();
93
		$qb
94
			->select('*')
95
			->from($this->tableName)
96
			->where($qb->expr()->in('user_id', $qb->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
97
98
		return $this->findEntities($qb);
99
	}
100
101
	/**
102
	 * Clear all statuses older than a given timestamp
103
	 *
104
	 * @param int $timestamp
105
	 */
106
	public function clearOlderThan(int $timestamp): void {
107
		$qb = $this->db->getQueryBuilder();
108
		$qb->update($this->tableName)
109
			->set('message_id', $qb->createNamedParameter(null))
110
			->set('custom_icon', $qb->createNamedParameter(null))
111
			->set('custom_message', $qb->createNamedParameter(null))
112
			->set('clear_at', $qb->createNamedParameter(null))
113
			->where($qb->expr()->isNotNull('clear_at'))
114
			->andWhere($qb->expr()->lte('clear_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT)));
115
116
		$qb->execute();
117
	}
118
}
119