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:
Complex classes like ShareByMailProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ShareByMailProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class ShareByMailProvider implements IShareProvider { |
||
55 | |||
56 | /** @var IDBConnection */ |
||
57 | private $dbConnection; |
||
58 | |||
59 | /** @var ILogger */ |
||
60 | private $logger; |
||
61 | |||
62 | /** @var ISecureRandom */ |
||
63 | private $secureRandom; |
||
64 | |||
65 | /** @var IUserManager */ |
||
66 | private $userManager; |
||
67 | |||
68 | /** @var IRootFolder */ |
||
69 | private $rootFolder; |
||
70 | |||
71 | /** @var IL10N */ |
||
72 | private $l; |
||
73 | |||
74 | /** @var IMailer */ |
||
75 | private $mailer; |
||
76 | |||
77 | /** @var IURLGenerator */ |
||
78 | private $urlGenerator; |
||
79 | |||
80 | /** @var IManager */ |
||
81 | private $activityManager; |
||
82 | |||
83 | /** @var SettingsManager */ |
||
84 | private $settingsManager; |
||
85 | |||
86 | /** @var Defaults */ |
||
87 | private $defaults; |
||
88 | |||
89 | /** @var IHasher */ |
||
90 | private $hasher; |
||
91 | |||
92 | /** @var CapabilitiesManager */ |
||
93 | private $capabilitiesManager; |
||
94 | |||
95 | /** |
||
96 | * Return the identifier of this provider. |
||
97 | * |
||
98 | * @return string Containing only [a-zA-Z0-9] |
||
99 | */ |
||
100 | public function identifier() { |
||
103 | |||
104 | /** |
||
105 | * DefaultShareProvider constructor. |
||
106 | * |
||
107 | * @param IDBConnection $connection |
||
108 | * @param ISecureRandom $secureRandom |
||
109 | * @param IUserManager $userManager |
||
110 | * @param IRootFolder $rootFolder |
||
111 | * @param IL10N $l |
||
112 | * @param ILogger $logger |
||
113 | * @param IMailer $mailer |
||
114 | * @param IURLGenerator $urlGenerator |
||
115 | * @param IManager $activityManager |
||
116 | * @param SettingsManager $settingsManager |
||
117 | * @param Defaults $defaults |
||
118 | * @param IHasher $hasher |
||
119 | * @param CapabilitiesManager $capabilitiesManager |
||
120 | */ |
||
121 | public function __construct( |
||
122 | IDBConnection $connection, |
||
123 | ISecureRandom $secureRandom, |
||
124 | IUserManager $userManager, |
||
125 | IRootFolder $rootFolder, |
||
126 | IL10N $l, |
||
127 | ILogger $logger, |
||
128 | IMailer $mailer, |
||
129 | IURLGenerator $urlGenerator, |
||
130 | IManager $activityManager, |
||
131 | SettingsManager $settingsManager, |
||
132 | Defaults $defaults, |
||
133 | IHasher $hasher, |
||
134 | CapabilitiesManager $capabilitiesManager |
||
135 | ) { |
||
136 | $this->dbConnection = $connection; |
||
137 | $this->secureRandom = $secureRandom; |
||
138 | $this->userManager = $userManager; |
||
139 | $this->rootFolder = $rootFolder; |
||
140 | $this->l = $l; |
||
141 | $this->logger = $logger; |
||
142 | $this->mailer = $mailer; |
||
143 | $this->urlGenerator = $urlGenerator; |
||
144 | $this->activityManager = $activityManager; |
||
145 | $this->settingsManager = $settingsManager; |
||
146 | $this->defaults = $defaults; |
||
147 | $this->hasher = $hasher; |
||
148 | $this->capabilitiesManager = $capabilitiesManager; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Share a path |
||
153 | * |
||
154 | * @param IShare $share |
||
155 | * @return IShare The share object |
||
156 | * @throws ShareNotFound |
||
157 | * @throws \Exception |
||
158 | */ |
||
159 | public function create(IShare $share) { |
||
193 | |||
194 | /** |
||
195 | * auto generate password in case of password enforcement on mail shares |
||
196 | * |
||
197 | * @param IShare $share |
||
198 | * @return string |
||
199 | * @throws \Exception |
||
200 | */ |
||
201 | protected function autoGeneratePassword($share) { |
||
226 | |||
227 | /** |
||
228 | * get password policy |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | protected function getPasswordPolicy() { |
||
240 | |||
241 | /** |
||
242 | * create activity if a file/folder was shared by mail |
||
243 | * |
||
244 | * @param IShare $share |
||
245 | */ |
||
246 | protected function createShareActivity(IShare $share) { |
||
273 | |||
274 | /** |
||
275 | * create activity if a file/folder was shared by mail |
||
276 | * |
||
277 | * @param IShare $share |
||
278 | * @param string $sharedWith |
||
279 | * @param bool $sendToSelf |
||
280 | */ |
||
281 | protected function createPasswordSendActivity(IShare $share, $sharedWith, $sendToSelf) { |
||
303 | |||
304 | |||
305 | /** |
||
306 | * publish activity if a file/folder was shared by mail |
||
307 | * |
||
308 | * @param $subject |
||
309 | * @param $parameters |
||
310 | * @param $affectedUser |
||
311 | * @param $fileId |
||
312 | * @param $filePath |
||
313 | */ |
||
314 | View Code Duplication | protected function publishActivity($subject, $parameters, $affectedUser, $fileId, $filePath) { |
|
324 | |||
325 | /** |
||
326 | * @param IShare $share |
||
327 | * @return int |
||
328 | * @throws \Exception |
||
329 | */ |
||
330 | protected function createMailShare(IShare $share) { |
||
367 | |||
368 | /** |
||
369 | * @param string $filename |
||
370 | * @param string $link |
||
371 | * @param string $initiator |
||
372 | * @param string $shareWith |
||
373 | * @param \DateTime|null $expiration |
||
374 | * @throws \Exception If mail couldn't be sent |
||
375 | */ |
||
376 | protected function sendMailNotification($filename, |
||
377 | $link, |
||
378 | $initiator, |
||
379 | $shareWith, |
||
380 | \DateTime $expiration = null) { |
||
381 | $initiatorUser = $this->userManager->get($initiator); |
||
382 | $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
||
383 | $message = $this->mailer->createMessage(); |
||
384 | |||
385 | $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [ |
||
386 | 'filename' => $filename, |
||
387 | 'link' => $link, |
||
388 | 'initiator' => $initiatorDisplayName, |
||
389 | 'expiration' => $expiration, |
||
390 | 'shareWith' => $shareWith, |
||
391 | ]); |
||
392 | |||
393 | $emailTemplate->setSubject($this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename))); |
||
394 | $emailTemplate->addHeader(); |
||
395 | $emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); |
||
396 | $text = $this->l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); |
||
397 | |||
398 | $emailTemplate->addBodyText( |
||
399 | $text . ' ' . $this->l->t('Click the button below to open it.'), |
||
400 | $text |
||
401 | ); |
||
402 | $emailTemplate->addBodyButton( |
||
403 | $this->l->t('Open »%s«', [$filename]), |
||
404 | $link |
||
405 | ); |
||
406 | |||
407 | $message->setTo([$shareWith]); |
||
408 | |||
409 | // The "From" contains the sharers name |
||
410 | $instanceName = $this->defaults->getName(); |
||
411 | $senderName = $this->l->t( |
||
412 | '%s via %s', |
||
413 | [ |
||
414 | $initiatorDisplayName, |
||
415 | $instanceName |
||
416 | ] |
||
417 | ); |
||
418 | $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
||
419 | |||
420 | // The "Reply-To" is set to the sharer if an mail address is configured |
||
421 | // also the default footer contains a "Do not reply" which needs to be adjusted. |
||
422 | $initiatorEmail = $initiatorUser->getEMailAddress(); |
||
423 | View Code Duplication | if($initiatorEmail !== null) { |
|
424 | $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); |
||
425 | $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')); |
||
426 | } else { |
||
427 | $emailTemplate->addFooter(); |
||
428 | } |
||
429 | |||
430 | $message->useTemplate($emailTemplate); |
||
431 | $this->mailer->send($message); |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * send password to recipient of a mail share |
||
436 | * |
||
437 | * @param IShare $share |
||
438 | * @param string $password |
||
439 | * @return bool |
||
440 | */ |
||
441 | protected function sendPassword(IShare $share, $password) { |
||
442 | |||
443 | $filename = $share->getNode()->getName(); |
||
444 | $initiator = $share->getSharedBy(); |
||
445 | $shareWith = $share->getSharedWith(); |
||
446 | |||
447 | if ($password === '' || $this->settingsManager->sendPasswordByMail() === false) { |
||
448 | return false; |
||
449 | } |
||
450 | |||
451 | $initiatorUser = $this->userManager->get($initiator); |
||
452 | $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
||
453 | $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null; |
||
454 | |||
455 | $plainBodyPart = $this->l->t("%s shared »%s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]); |
||
456 | $htmlBodyPart = $this->l->t('%s shared »%s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]); |
||
457 | |||
458 | $message = $this->mailer->createMessage(); |
||
459 | |||
460 | $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [ |
||
461 | 'filename' => $filename, |
||
462 | 'password' => $password, |
||
463 | 'initiator' => $initiatorDisplayName, |
||
464 | 'initiatorEmail' => $initiatorEmailAddress, |
||
465 | 'shareWith' => $shareWith, |
||
466 | ]); |
||
467 | |||
468 | $emailTemplate->setSubject($this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName])); |
||
469 | $emailTemplate->addHeader(); |
||
470 | $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false); |
||
471 | $emailTemplate->addBodyText($htmlBodyPart, $plainBodyPart); |
||
472 | $emailTemplate->addBodyText($this->l->t('It is protected with the following password: %s', [$password])); |
||
473 | |||
474 | // The "From" contains the sharers name |
||
475 | $instanceName = $this->defaults->getName(); |
||
476 | $senderName = $this->l->t( |
||
477 | '%s via %s', |
||
478 | [ |
||
479 | $initiatorDisplayName, |
||
480 | $instanceName |
||
481 | ] |
||
482 | ); |
||
483 | $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
||
484 | View Code Duplication | if ($initiatorEmailAddress !== null) { |
|
485 | $message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]); |
||
486 | $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); |
||
487 | } else { |
||
488 | $emailTemplate->addFooter(); |
||
489 | } |
||
490 | |||
491 | $message->setTo([$shareWith]); |
||
492 | $message->useTemplate($emailTemplate); |
||
493 | $this->mailer->send($message); |
||
494 | |||
495 | $this->createPasswordSendActivity($share, $shareWith, false); |
||
496 | |||
497 | return true; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * send auto generated password to the owner. This happens if the admin enforces |
||
502 | * a password for mail shares and forbid to send the password by mail to the recipient |
||
503 | * |
||
504 | * @param IShare $share |
||
505 | * @param string $password |
||
506 | * @return bool |
||
507 | * @throws \Exception |
||
508 | */ |
||
509 | protected function sendPasswordToOwner(IShare $share, $password) { |
||
553 | |||
554 | /** |
||
555 | * generate share token |
||
556 | * |
||
557 | * @return string |
||
558 | */ |
||
559 | protected function generateToken($size = 15) { |
||
563 | |||
564 | /** |
||
565 | * Get all children of this share |
||
566 | * |
||
567 | * @param IShare $parent |
||
568 | * @return IShare[] |
||
569 | */ |
||
570 | View Code Duplication | public function getChildren(IShare $parent) { |
|
588 | |||
589 | /** |
||
590 | * add share to the database and return the ID |
||
591 | * |
||
592 | * @param int $itemSource |
||
593 | * @param string $itemType |
||
594 | * @param string $shareWith |
||
595 | * @param string $sharedBy |
||
596 | * @param string $uidOwner |
||
597 | * @param int $permissions |
||
598 | * @param string $token |
||
599 | * @return int |
||
600 | */ |
||
601 | protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $password) { |
||
627 | |||
628 | /** |
||
629 | * Update a share |
||
630 | * |
||
631 | * @param IShare $share |
||
632 | * @param string|null $plainTextPassword |
||
633 | * @return IShare The share object |
||
634 | */ |
||
635 | public function update(IShare $share, $plainTextPassword = null) { |
||
660 | |||
661 | /** |
||
662 | * @inheritdoc |
||
663 | */ |
||
664 | public function move(IShare $share, $recipient) { |
||
670 | |||
671 | /** |
||
672 | * Delete a share (owner unShares the file) |
||
673 | * |
||
674 | * @param IShare $share |
||
675 | */ |
||
676 | public function delete(IShare $share) { |
||
679 | |||
680 | /** |
||
681 | * @inheritdoc |
||
682 | */ |
||
683 | public function deleteFromSelf(IShare $share, $recipient) { |
||
687 | |||
688 | /** |
||
689 | * @inheritdoc |
||
690 | */ |
||
691 | View Code Duplication | public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
743 | |||
744 | /** |
||
745 | * @inheritdoc |
||
746 | */ |
||
747 | View Code Duplication | public function getShareById($id, $recipientId = null) { |
|
771 | |||
772 | /** |
||
773 | * Get shares for a given path |
||
774 | * |
||
775 | * @param \OCP\Files\Node $path |
||
776 | * @return IShare[] |
||
777 | */ |
||
778 | View Code Duplication | public function getSharesByPath(Node $path) { |
|
795 | |||
796 | /** |
||
797 | * @inheritdoc |
||
798 | */ |
||
799 | View Code Duplication | public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
835 | |||
836 | /** |
||
837 | * Get a share by token |
||
838 | * |
||
839 | * @param string $token |
||
840 | * @return IShare |
||
841 | * @throws ShareNotFound |
||
842 | */ |
||
843 | View Code Duplication | public function getShareByToken($token) { |
|
866 | |||
867 | /** |
||
868 | * remove share from table |
||
869 | * |
||
870 | * @param string $shareId |
||
871 | */ |
||
872 | protected function removeShareFromTable($shareId) { |
||
878 | |||
879 | /** |
||
880 | * Create a share object from an database row |
||
881 | * |
||
882 | * @param array $data |
||
883 | * @return IShare |
||
884 | * @throws InvalidShare |
||
885 | * @throws ShareNotFound |
||
886 | */ |
||
887 | protected function createShareObject($data) { |
||
929 | |||
930 | /** |
||
931 | * Get the node with file $id for $user |
||
932 | * |
||
933 | * @param string $userId |
||
934 | * @param int $id |
||
935 | * @return \OCP\Files\File|\OCP\Files\Folder |
||
936 | * @throws InvalidShare |
||
937 | */ |
||
938 | View Code Duplication | private function getNode($userId, $id) { |
|
953 | |||
954 | /** |
||
955 | * A user is deleted from the system |
||
956 | * So clean up the relevant shares. |
||
957 | * |
||
958 | * @param string $uid |
||
959 | * @param int $shareType |
||
960 | */ |
||
961 | View Code Duplication | public function userDeleted($uid, $shareType) { |
|
969 | |||
970 | /** |
||
971 | * This provider does not support group shares |
||
972 | * |
||
973 | * @param string $gid |
||
974 | */ |
||
975 | public function groupDeleted($gid) { |
||
978 | |||
979 | /** |
||
980 | * This provider does not support group shares |
||
981 | * |
||
982 | * @param string $uid |
||
983 | * @param string $gid |
||
984 | */ |
||
985 | public function userDeletedFromGroup($uid, $gid) { |
||
988 | |||
989 | /** |
||
990 | * get database row of a give share |
||
991 | * |
||
992 | * @param $id |
||
993 | * @return array |
||
994 | * @throws ShareNotFound |
||
995 | */ |
||
996 | protected function getRawShare($id) { |
||
1014 | |||
1015 | View Code Duplication | public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
1055 | |||
1056 | /** |
||
1057 | * @inheritdoc |
||
1058 | */ |
||
1059 | public function getAccessList($nodes, $currentAccess) { |
||
1082 | |||
1083 | } |
||
1084 |