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 |
||
32 | private IManager $manager; |
||
|
|||
33 | private IURLGenerator $urlGenerator; |
||
34 | |||
35 | public function __construct(IManager $IManager, IURLGenerator $urlGenerator) { |
||
36 | $this->manager = $IManager; |
||
37 | $this->urlGenerator = $urlGenerator; |
||
38 | } |
||
39 | |||
40 | function credentialExpiredNotification($credential) { |
||
41 | $link = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'index.php/apps/passman/#/vault/' . $credential->getVaultId() . '/edit/' . $credential->getId())); |
||
42 | $api = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'index.php/apps/passman')); |
||
43 | $notification = $this->manager->createNotification(); |
||
44 | $remindAction = $notification->createAction(); |
||
45 | $remindAction->setLabel('remind') |
||
46 | ->setLink($api . '/api/internal/notifications/remind/' . $credential->getId(), 'POST'); |
||
47 | |||
48 | $declineAction = $notification->createAction(); |
||
49 | $declineAction->setLabel('ignore') |
||
50 | ->setLink($api . '/api/internal/notifications/read/' . $credential->getId(), 'DELETE'); |
||
51 | |||
52 | $notification->setApp('passman') |
||
53 | ->setUser($credential->getUserId()) |
||
54 | ->setDateTime(new \DateTime()) |
||
55 | ->setObject('credential', $credential->getId()) // Set notification type and id |
||
56 | ->setSubject('credential_expired', [$credential->getLabel()]) // set subject and parameters |
||
57 | ->setLink($link) |
||
58 | ->addAction($declineAction) |
||
59 | ->addAction($remindAction); |
||
60 | |||
61 | $this->manager->notify($notification); |
||
62 | } |
||
63 | |||
64 | |||
65 | function credentialSharedNotification($data) { |
||
66 | $link = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'index.php/apps/passman/#/')); |
||
67 | $api = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkTo('', 'index.php/apps/passman')); |
||
68 | $notification = $this->manager->createNotification(); |
||
69 | |||
70 | $declineAction = $notification->createAction(); |
||
71 | $declineAction->setLabel('decline') |
||
72 | ->setLink($api . '/api/v2/sharing/decline/' . $data['req_id'], 'DELETE'); |
||
73 | |||
74 | $notification->setApp('passman') |
||
75 | ->setUser($data['target_user']) |
||
76 | ->setDateTime(new \DateTime()) |
||
77 | ->setObject('passman_share_request', $data['req_id']) // type and id |
||
78 | ->setSubject('credential_shared', [$data['from_user'], $data['credential_label']]) // subject and parameters |
||
79 | ->setLink($link) |
||
80 | ->addAction($declineAction); |
||
81 | |||
82 | $this->manager->notify($notification); |
||
83 | } |
||
84 | |||
85 | |||
86 | function credentialDeclinedSharedNotification($data) { |
||
87 | $notification = $this->manager->createNotification(); |
||
88 | $notification->setApp('passman') |
||
89 | ->setUser($data['target_user']) |
||
90 | ->setDateTime(new \DateTime()) |
||
91 | ->setObject('passman_share_request', $data['req_id']) // type and id |
||
92 | ->setSubject('credential_share_denied', [$data['from_user'], $data['credential_label']]); // subject and parameters |
||
93 | $this->manager->notify($notification); |
||
94 | } |
||
95 | |||
96 | |||
97 | function credentialAcceptedSharedNotification($data) { |
||
98 | $notification = $this->manager->createNotification(); |
||
99 | $notification->setApp('passman') |
||
100 | ->setUser($data['target_user']) |
||
101 | ->setDateTime(new \DateTime()) |
||
102 | ->setObject('passman_share_request', $data['req_id']) // type and id |
||
103 | ->setSubject('credential_share_accepted', [$data['from_user'], $data['credential_label']]); // subject and parameters |
||
104 | $this->manager->notify($notification); |
||
105 | } |
||
106 | |||
107 | } |
||
108 |