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 |
||
33 | class BoardMapper extends DeckMapper implements IPermissionMapper { |
||
34 | |||
35 | private $labelMapper; |
||
36 | private $aclMapper; |
||
37 | private $stackMapper; |
||
38 | private $userManager; |
||
39 | private $groupManager; |
||
40 | |||
41 | private $circlesEnabled; |
||
42 | |||
43 | public function __construct( |
||
44 | IDBConnection $db, |
||
45 | LabelMapper $labelMapper, |
||
46 | AclMapper $aclMapper, |
||
47 | StackMapper $stackMapper, |
||
48 | IUserManager $userManager, |
||
49 | IGroupManager $groupManager |
||
50 | ) { |
||
51 | parent::__construct($db, 'deck_boards', Board::class); |
||
|
|||
52 | $this->labelMapper = $labelMapper; |
||
53 | $this->aclMapper = $aclMapper; |
||
54 | $this->stackMapper = $stackMapper; |
||
55 | $this->userManager = $userManager; |
||
56 | $this->groupManager = $groupManager; |
||
57 | |||
58 | $this->circlesEnabled = \OC::$server->getAppManager()->isEnabledForUser('circles'); |
||
59 | } |
||
60 | |||
61 | |||
62 | /** |
||
63 | * @param $id |
||
64 | * @param bool $withLabels |
||
65 | * @param bool $withAcl |
||
66 | * @return \OCP\AppFramework\Db\Entity |
||
67 | * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException |
||
68 | * @throws DoesNotExistException |
||
69 | */ |
||
70 | public function find($id, $withLabels = false, $withAcl = false) { |
||
71 | $sql = 'SELECT id, title, owner, color, archived, deleted_at, last_modified FROM `*PREFIX*deck_boards` ' . |
||
72 | 'WHERE `id` = ?'; |
||
73 | $board = $this->findEntity($sql, [$id]); |
||
74 | |||
75 | // Add labels |
||
76 | if ($withLabels) { |
||
77 | $labels = $this->labelMapper->findAll($id); |
||
78 | $board->setLabels($labels); |
||
79 | } |
||
80 | |||
81 | // Add acl |
||
82 | if ($withAcl) { |
||
83 | $acl = $this->aclMapper->findAll($id); |
||
84 | $board->setAcl($acl); |
||
85 | } |
||
86 | |||
87 | return $board; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Find all boards for a given user |
||
92 | * |
||
93 | * @param $userId |
||
94 | * @param null $limit |
||
95 | * @param null $offset |
||
96 | * @return array |
||
97 | */ |
||
98 | public function findAllByUser($userId, $limit = null, $offset = null, $since = -1) { |
||
99 | $sql = 'SELECT id, title, owner, color, archived, deleted_at, 0 as shared, last_modified FROM `*PREFIX*deck_boards` WHERE owner = ? AND last_modified > ? UNION ' . |
||
100 | 'SELECT boards.id, title, owner, color, archived, deleted_at, 1 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' . |
||
101 | 'JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=? AND boards.owner != ? AND last_modified > ?'; |
||
102 | $entries = $this->findEntities($sql, [$userId, $since, $userId, Acl::PERMISSION_TYPE_USER, $userId, $since], $limit, $offset); |
||
103 | /* @var Board $entry */ |
||
104 | foreach ($entries as $entry) { |
||
105 | $acl = $this->aclMapper->findAll($entry->id); |
||
106 | $entry->setAcl($acl); |
||
107 | } |
||
108 | return $entries; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Find all boards for a given user |
||
113 | * |
||
114 | * @param $userId |
||
115 | * @param $groups |
||
116 | * @param null $limit |
||
117 | * @param null $offset |
||
118 | * @return array |
||
119 | */ |
||
120 | public function findAllByGroups($userId, $groups, $limit = null, $offset = null) { |
||
121 | if (count($groups) <= 0) { |
||
122 | return []; |
||
123 | } |
||
124 | $sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' . |
||
125 | 'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND ('; |
||
126 | View Code Duplication | for ($i = 0, $iMax = count($groups); $i < $iMax; $i++) { |
|
127 | $sql .= 'acl.participant = ? '; |
||
128 | if (count($groups) > 1 && $i < count($groups) - 1) { |
||
129 | $sql .= ' OR '; |
||
130 | } |
||
131 | } |
||
132 | $sql .= ');'; |
||
133 | $entries = $this->findEntities($sql, array_merge([$userId, Acl::PERMISSION_TYPE_GROUP], $groups), $limit, $offset); |
||
134 | /* @var Board $entry */ |
||
135 | foreach ($entries as $entry) { |
||
136 | $acl = $this->aclMapper->findAll($entry->id); |
||
137 | $entry->setAcl($acl); |
||
138 | } |
||
139 | return $entries; |
||
140 | } |
||
141 | |||
142 | public function findAllByCircles($userId, $limit = null, $offset = null) { |
||
143 | if (!$this->circlesEnabled) { |
||
144 | return []; |
||
145 | } |
||
146 | $circles = array_map(function($circle) { |
||
147 | return $circle->getUniqueId(); |
||
148 | }, \OCA\Circles\Api\v1\Circles::joinedCircles('', true)); |
||
149 | if (count($circles) === 0) { |
||
150 | return []; |
||
151 | } |
||
152 | |||
153 | $sql = 'SELECT boards.id, title, owner, color, archived, deleted_at, 2 as shared, last_modified FROM `*PREFIX*deck_boards` as boards ' . |
||
154 | 'INNER JOIN `*PREFIX*deck_board_acl` as acl ON boards.id=acl.board_id WHERE owner != ? AND type=? AND ('; |
||
155 | View Code Duplication | for ($i = 0, $iMax = count($circles); $i < $iMax; $i++) { |
|
156 | $sql .= 'acl.participant = ? '; |
||
157 | if (count($circles) > 1 && $i < count($circles) - 1) { |
||
158 | $sql .= ' OR '; |
||
159 | } |
||
160 | } |
||
161 | $sql .= ');'; |
||
162 | $entries = $this->findEntities($sql, array_merge([$userId, Acl::PERMISSION_TYPE_CIRCLE], $circles), $limit, $offset); |
||
163 | /* @var Board $entry */ |
||
164 | foreach ($entries as $entry) { |
||
165 | $acl = $this->aclMapper->findAll($entry->id); |
||
166 | $entry->setAcl($acl); |
||
167 | } |
||
168 | return $entries; |
||
169 | } |
||
170 | |||
171 | public function findAll() { |
||
175 | |||
176 | public function findToDelete() { |
||
183 | |||
184 | public function delete(/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ |
||
205 | |||
206 | public function isOwner($userId, $boardId) { |
||
210 | |||
211 | public function findBoardId($id) { |
||
214 | |||
215 | public function mapAcl(Acl &$acl) { |
||
216 | $userManager = $this->userManager; |
||
217 | $groupManager = $this->groupManager; |
||
218 | $acl->resolveRelation('participant', function($participant) use (&$acl, &$userManager, &$groupManager) { |
||
219 | View Code Duplication | if ($acl->getType() === Acl::PERMISSION_TYPE_USER) { |
|
249 | |||
250 | /** |
||
251 | * @param Board $board |
||
252 | */ |
||
253 | View Code Duplication | public function mapOwner(Board &$board) { |
|
263 | |||
264 | |||
265 | } |
||
266 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.