Failed Conditions
Pull Request — master (#682)
by
unknown
09:41
created

lib/Db/CredentialRevisionMapper.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Passman\Db;
25
26
use OCA\Passman\Utility\Utils;
27
use OCP\AppFramework\Db\DoesNotExistException;
28
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
29
use OCP\AppFramework\Db\QBMapper;
30
use OCP\DB\QueryBuilder\IQueryBuilder;
31
use OCP\IDBConnection;
32
33
class CredentialRevisionMapper extends QBMapper {
34
	const TABLE_NAME = 'passman_revisions';
35
	private Utils $utils;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
36
37
	public function __construct(IDBConnection $db, Utils $utils) {
38
		parent::__construct($db, self::TABLE_NAME);
39
		$this->utils = $utils;
40
	}
41
42
43
	/**
44
	 * Get revisions from a credential
45
	 *
46
	 * @param int $credential_id
47
	 * @param string|null $user_id
48
	 * @return CredentialRevision[]
49
	 */
50
	public function getRevisions(int $credential_id, string $user_id = null) {
51
		$qb = $this->db->getQueryBuilder();
52
		$qb->select('*')
53
			->from(self::TABLE_NAME)
54
			->where($qb->expr()->eq('credential_id', $qb->createNamedParameter($credential_id, IQueryBuilder::PARAM_INT)));
55
56
		if ($user_id !== null) {
57
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
58
		}
59
60
		/** @var CredentialRevision[] $credentialRevisions */
61
		$credentialRevisions = $this->findEntities($qb);
62
		return $credentialRevisions;
63
	}
64
65
	/**
66
	 * @param int $revision_id
67
	 * @param string|null $user_id
68
	 * @return CredentialRevision
69
	 * @throws DoesNotExistException
70
	 * @throws MultipleObjectsReturnedException
71
	 */
72
	public function getRevision(int $revision_id, string $user_id = null) {
73
		$qb = $this->db->getQueryBuilder();
74
		$qb->select('*')
75
			->from(self::TABLE_NAME)
76
			->where($qb->expr()->eq('id', $qb->createNamedParameter($revision_id, IQueryBuilder::PARAM_INT)));
77
78
		if ($user_id !== null) {
79
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
80
		}
81
82
		/** @var CredentialRevision $credentialRevision */
83
		$credentialRevision = $this->findEntity($qb);
84
		return $credentialRevision;
85
	}
86
87
	/**
88
	 * Create a revision
89
	 * @param $credential
90
	 * @param $userId
91
	 * @param $credential_id
92
	 * @param $edited_by
93
	 * @return CredentialRevision
94
	 */
95
	public function create($credential, $userId, $credential_id, $edited_by) {
96
		$revision = new CredentialRevision();
97
		$revision->setGuid($this->utils->GUID());
98
		$revision->setUserId($userId);
99
		$revision->setCreated($this->utils->getTime());
100
		$revision->setCredentialId($credential_id);
101
		$revision->setEditedBy($edited_by);
102
		$revision->setCredentialData(base64_encode(json_encode($credential)));
103
		return $this->insert($revision);
104
	}
105
106
107
	/**
108
	 * Delete a revision
109
	 * @param $revision_id
110
	 * @param $user_id
111
	 * @return CredentialRevision
112
	 */
113
	public function deleteRevision($revision_id, $user_id) {
114
		$revision = new CredentialRevision();
115
		$revision->setId($revision_id);
116
		$revision->setUserId($user_id);
117
		return $this->delete($revision);
118
	}
119
}
120