Failed Conditions
Push — master ( e8410d...a45585 )
by Marcos
09:36 queued 11s
created

CredentialRevisionMapper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 20
loc 75
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Entity;
29
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
30
use OCP\AppFramework\Db\QBMapper;
31
use OCP\DB\QueryBuilder\IQueryBuilder;
32
use OCP\IDBConnection;
33
34
class CredentialRevisionMapper extends QBMapper {
35
	const TABLE_NAME = 'passman_revisions';
36
	private Utils $utils;
0 ignored issues
show
Bug introduced by
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...
37
38
	public function __construct(IDBConnection $db, Utils $utils) {
39
		parent::__construct($db, self::TABLE_NAME);
40
		$this->utils = $utils;
41
	}
42
43
44
	/**
45
	 * Get revisions from a credential
46
	 *
47
	 * @param int $credential_id
48
	 * @param string|null $user_id
49
	 * @return Entity[]
50
	 */
51
	public function getRevisions(int $credential_id, string $user_id = null) {
52
		$qb = $this->db->getQueryBuilder();
53
		$qb->select('*')
54
			->from(self::TABLE_NAME)
55
			->where($qb->expr()->eq('credential_id', $qb->createNamedParameter($credential_id, IQueryBuilder::PARAM_INT)));
56
57
		if ($user_id !== null) {
58
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
59
		}
60
61
		return $this->findEntities($qb);
62
	}
63
64
	/**
65
	 * @param int $revision_id
66
	 * @param string|null $user_id
67
	 * @return Entity
68
	 * @throws DoesNotExistException
69
	 * @throws MultipleObjectsReturnedException
70
	 */
71
	public function getRevision(int $revision_id, string $user_id = null) {
72
		$qb = $this->db->getQueryBuilder();
73
		$qb->select('*')
74
			->from(self::TABLE_NAME)
75
			->where($qb->expr()->eq('id', $qb->createNamedParameter($revision_id, IQueryBuilder::PARAM_INT)));
76
77
		if ($user_id !== null) {
78
			$qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR)));
79
		}
80
81
		return $this->findEntity($qb);
82
	}
83
84
	/**
85
	 * Create a revision
86
	 * @param $credential
87
	 * @param $userId
88
	 * @param $credential_id
89
	 * @param $edited_by
90
	 * @return CredentialRevision
91
	 */
92
	public function create($credential, $userId, $credential_id, $edited_by) {
93
		$revision = new CredentialRevision();
94
		$revision->setGuid($this->utils->GUID());
95
		$revision->setUserId($userId);
96
		$revision->setCreated($this->utils->getTime());
97
		$revision->setCredentialId($credential_id);
98
		$revision->setEditedBy($edited_by);
99
		$revision->setCredentialData(base64_encode(json_encode($credential)));
100
		return $this->insert($revision);
101
	}
102
103
104
	/**
105
	 * Delete a revision
106
	 * @param $revision_id
107
	 * @param $user_id
108
	 * @return CredentialRevision
109
	 */
110
	public function deleteRevision($revision_id, $user_id) {
111
		$revision = new CredentialRevision();
112
		$revision->setId($revision_id);
113
		$revision->setUserId($user_id);
114
		return $this->delete($revision);
115
	}
116
}
117