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 |
||
39 | class FedShareManager { |
||
40 | const ACTION_URL = 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/'; |
||
41 | |||
42 | /** |
||
43 | * @var FederatedShareProvider |
||
44 | */ |
||
45 | private $federatedShareProvider; |
||
46 | |||
47 | /** |
||
48 | * @var Notifications |
||
49 | */ |
||
50 | private $notifications; |
||
51 | |||
52 | /** |
||
53 | * @var IUserManager |
||
54 | */ |
||
55 | private $userManager; |
||
56 | |||
57 | /** |
||
58 | * @var ActivityManager |
||
59 | */ |
||
60 | private $activityManager; |
||
61 | |||
62 | /** |
||
63 | * @var NotificationManager |
||
64 | */ |
||
65 | private $notificationManager; |
||
66 | |||
67 | /** |
||
68 | * @var AddressHandler |
||
69 | */ |
||
70 | private $addressHandler; |
||
71 | |||
72 | /** |
||
73 | * @var Permissions |
||
74 | */ |
||
75 | private $permissions; |
||
76 | |||
77 | /** |
||
78 | * @var EventDispatcherInterface |
||
79 | */ |
||
80 | private $eventDispatcher; |
||
81 | |||
82 | /** |
||
83 | * FedShareManager constructor. |
||
84 | * |
||
85 | * @param FederatedShareProvider $federatedShareProvider |
||
86 | * @param Notifications $notifications |
||
87 | * @param IUserManager $userManager |
||
88 | * @param ActivityManager $activityManager |
||
89 | * @param NotificationManager $notificationManager |
||
90 | * @param AddressHandler $addressHandler |
||
91 | * @param Permissions $permissions |
||
92 | * @param EventDispatcherInterface $eventDispatcher |
||
93 | */ |
||
94 | public function __construct(FederatedShareProvider $federatedShareProvider, |
||
112 | |||
113 | /** |
||
114 | * Create an incoming share |
||
115 | * |
||
116 | * @param Address $ownerAddress |
||
117 | * @param Address $sharedByAddress |
||
118 | * @param string $shareWith |
||
119 | * @param int $remoteId |
||
120 | * @param string $name |
||
121 | * @param string $token |
||
122 | * |
||
123 | * @return void |
||
124 | */ |
||
125 | public function createShare(Address $ownerAddress, |
||
181 | |||
182 | /** |
||
183 | * @param IShare $share |
||
184 | * @param int $remoteId |
||
185 | * @param string $shareWith |
||
186 | * @param int|null $permissions - null for OCM 1.0-proposal1 |
||
187 | * |
||
188 | * @return IShare |
||
189 | * |
||
190 | * @throws \OCP\Share\Exceptions\ShareNotFound |
||
191 | */ |
||
192 | public function reShare(IShare $share, $remoteId, $shareWith, $permissions = null) { |
||
206 | |||
207 | /** |
||
208 | * |
||
209 | * |
||
210 | * @param IShare $share |
||
211 | * |
||
212 | * @throws \OCP\Files\InvalidPathException |
||
213 | * @throws \OCP\Files\NotFoundException |
||
214 | */ |
||
215 | View Code Duplication | public function acceptShare(IShare $share) { |
|
230 | |||
231 | /** |
||
232 | * Delete declined share and create a activity |
||
233 | * |
||
234 | * @param IShare $share |
||
235 | * |
||
236 | * @throws \OCP\Files\InvalidPathException |
||
237 | * @throws \OCP\Files\NotFoundException |
||
238 | */ |
||
239 | View Code Duplication | public function declineShare(IShare $share) { |
|
255 | |||
256 | /** |
||
257 | * Unshare an item from self |
||
258 | * |
||
259 | * @param int $id |
||
260 | * @param string $token |
||
261 | * |
||
262 | * @return void |
||
263 | */ |
||
264 | public function unshare($id, $token) { |
||
290 | |||
291 | /** |
||
292 | * @param IShare $share |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | public function undoReshare(IShare $share) { |
||
299 | |||
300 | /** |
||
301 | * Update permissions |
||
302 | * |
||
303 | * @param IShare $share |
||
304 | * @param string[] $ocmPermissions as ['read', 'write', 'share'] |
||
305 | * |
||
306 | * @return void |
||
307 | */ |
||
308 | public function updateOcmPermissions(IShare $share, $ocmPermissions) { |
||
312 | |||
313 | /** |
||
314 | * Update permissions |
||
315 | * |
||
316 | * @param IShare $share |
||
317 | * @param int $permissions |
||
318 | * |
||
319 | * @return void |
||
320 | */ |
||
321 | public function updatePermissions(IShare $share, $permissions) { |
||
327 | |||
328 | /** |
||
329 | * @param IShare $share |
||
330 | * @param callable $callback |
||
331 | * |
||
332 | * @throws \OCP\Share\Exceptions\ShareNotFound |
||
333 | * @throws \OC\HintException |
||
334 | */ |
||
335 | protected function notifyRemote($share, $callback) { |
||
348 | |||
349 | /** |
||
350 | * Publish a new activity |
||
351 | * |
||
352 | * @param string $affectedUser |
||
353 | * @param string $subject |
||
354 | * @param array $subjectParams |
||
355 | * @param string $objectType |
||
356 | * @param int $objectId |
||
357 | * @param string $objectName |
||
358 | * @param string $link |
||
359 | * |
||
360 | * @return void |
||
361 | */ |
||
362 | protected function publishActivity($affectedUser, |
||
379 | |||
380 | /** |
||
381 | * Get a new notification |
||
382 | * |
||
383 | * @param string $uid |
||
384 | * |
||
385 | * @return \OCP\Notification\INotification |
||
386 | */ |
||
387 | protected function createNotification($uid) { |
||
393 | |||
394 | /** |
||
395 | * @param int $shareId |
||
396 | * @return string |
||
397 | */ |
||
398 | protected function getActionLink($shareId) { |
||
405 | |||
406 | /** |
||
407 | * Get file |
||
408 | * |
||
409 | * @param string $user |
||
410 | * @param int $fileSource |
||
411 | * |
||
412 | * @return array with internal path of the file and a absolute link to it |
||
413 | */ |
||
414 | protected function getFile($user, $fileSource) { |
||
430 | |||
431 | /** |
||
432 | * Check if we are the initiator or the owner of a re-share |
||
433 | * and return the correct UID |
||
434 | * |
||
435 | * @param IShare $share |
||
436 | * |
||
437 | * @return string |
||
438 | */ |
||
439 | protected function getCorrectUid(IShare $share) { |
||
446 | } |
||
447 |