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 |
||
38 | class FedShareManager { |
||
39 | const ACTION_URL = 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/'; |
||
40 | |||
41 | /** |
||
42 | * @var FederatedShareProvider |
||
43 | */ |
||
44 | private $federatedShareProvider; |
||
45 | |||
46 | /** |
||
47 | * @var Notifications |
||
48 | */ |
||
49 | private $notifications; |
||
50 | |||
51 | /** |
||
52 | * @var IUserManager |
||
53 | */ |
||
54 | private $userManager; |
||
55 | |||
56 | /** |
||
57 | * @var ActivityManager |
||
58 | */ |
||
59 | private $activityManager; |
||
60 | |||
61 | /** |
||
62 | * @var NotificationManager |
||
63 | */ |
||
64 | private $notificationManager; |
||
65 | |||
66 | /** |
||
67 | * @var AddressHandler |
||
68 | */ |
||
69 | private $addressHandler; |
||
70 | |||
71 | /** |
||
72 | * @var EventDispatcherInterface |
||
73 | */ |
||
74 | private $eventDispatcher; |
||
75 | |||
76 | /** |
||
77 | * FedShareManager constructor. |
||
78 | * |
||
79 | * @param FederatedShareProvider $federatedShareProvider |
||
80 | * @param Notifications $notifications |
||
81 | * @param IUserManager $userManager |
||
82 | * @param ActivityManager $activityManager |
||
83 | * @param NotificationManager $notificationManager |
||
84 | * @param AddressHandler $addressHandler |
||
85 | * @param EventDispatcherInterface $eventDispatcher |
||
86 | */ |
||
87 | public function __construct(FederatedShareProvider $federatedShareProvider, |
||
103 | |||
104 | /** |
||
105 | * Create an incoming share |
||
106 | * |
||
107 | * @param string $shareWith |
||
108 | * @param string $remote |
||
109 | * @param int $remoteId |
||
110 | * @param string $owner |
||
111 | * @param string $name |
||
112 | * @param int $ownerFederatedId |
||
113 | * @param int $sharedByFederatedId |
||
114 | * @param string $sharedBy |
||
115 | * @param string $token |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | public function createShare($shareWith, |
||
182 | |||
183 | /** |
||
184 | * @param IShare $share |
||
185 | * @param int $remoteId |
||
186 | * @param string $shareWith |
||
187 | * @param int $permissions |
||
188 | * |
||
189 | * @return IShare |
||
190 | * |
||
191 | * @throws \OCP\Share\Exceptions\ShareNotFound |
||
192 | */ |
||
193 | public function reShare(IShare $share, $remoteId, $shareWith, $permissions) { |
||
205 | |||
206 | /** |
||
207 | * |
||
208 | * |
||
209 | * @param IShare $share |
||
210 | * |
||
211 | * @throws \OCP\Files\InvalidPathException |
||
212 | * @throws \OCP\Files\NotFoundException |
||
213 | */ |
||
214 | View Code Duplication | public function acceptShare(IShare $share) { |
|
229 | |||
230 | /** |
||
231 | * Delete declined share and create a activity |
||
232 | * |
||
233 | * @param IShare $share |
||
234 | * |
||
235 | * @throws \OCP\Files\InvalidPathException |
||
236 | * @throws \OCP\Files\NotFoundException |
||
237 | */ |
||
238 | View Code Duplication | public function declineShare(IShare $share) { |
|
254 | |||
255 | /** |
||
256 | * Unshare an item |
||
257 | * |
||
258 | * @param int $id |
||
259 | * @param string $token |
||
260 | * |
||
261 | * @return void |
||
262 | */ |
||
263 | public function unshare($id, $token) { |
||
290 | |||
291 | /** |
||
292 | * @param IShare $share |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | public function revoke(IShare $share) { |
||
299 | |||
300 | /** |
||
301 | * Update permissions |
||
302 | * |
||
303 | * @param IShare $share |
||
304 | * @param int $permissions |
||
305 | * |
||
306 | * @return void |
||
307 | */ |
||
308 | public function updatePermissions(IShare $share, $permissions) { |
||
312 | |||
313 | /** |
||
314 | * @param IShare $share |
||
315 | * @param callable $callback |
||
316 | * |
||
317 | * @throws \OCP\Share\Exceptions\ShareNotFound |
||
318 | * @throws \OC\HintException |
||
319 | */ |
||
320 | protected function notifyRemote($share, $callback) { |
||
329 | |||
330 | /** |
||
331 | * Publish a new activity |
||
332 | * |
||
333 | * @param string $affectedUser |
||
334 | * @param string $subject |
||
335 | * @param array $subjectParams |
||
336 | * @param string $objectType |
||
337 | * @param int $objectId |
||
338 | * @param string $objectName |
||
339 | * @param string $link |
||
340 | * |
||
341 | * @return void |
||
342 | */ |
||
343 | protected function publishActivity($affectedUser, |
||
360 | |||
361 | /** |
||
362 | * Get a new notification |
||
363 | * |
||
364 | * @param string $uid |
||
365 | * |
||
366 | * @return \OCP\Notification\INotification |
||
367 | */ |
||
368 | protected function createNotification($uid) { |
||
374 | |||
375 | /** |
||
376 | * @param int $shareId |
||
377 | * @return string |
||
378 | */ |
||
379 | protected function getActionLink($shareId) { |
||
386 | |||
387 | /** |
||
388 | * Get file |
||
389 | * |
||
390 | * @param string $user |
||
391 | * @param int $fileSource |
||
392 | * |
||
393 | * @return array with internal path of the file and a absolute link to it |
||
394 | */ |
||
395 | protected function getFile($user, $fileSource) { |
||
411 | |||
412 | /** |
||
413 | * Check if we are the initiator or the owner of a re-share |
||
414 | * and return the correct UID |
||
415 | * |
||
416 | * @param IShare $share |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | protected function getCorrectUid(IShare $share) { |
||
427 | } |
||
428 |