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 |
||
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; |
||
|
|||
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 |
||
117 |