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 AzineNotifierService 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 AzineNotifierService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class AzineNotifierService implements NotifierServiceInterface |
||
18 | { |
||
19 | /** |
||
20 | * Override this function to fill in any non-recipient-specific parameters that are required to |
||
21 | * render the notifications-template or one of the notification-item-templates that |
||
22 | * are rendered into the notifications-template |
||
23 | * |
||
24 | * @return array |
||
25 | */ |
||
26 | 3 | protected function getVarsForNotificationsEmail() |
|
32 | |||
33 | /** |
||
34 | * Override this function to fill in any recipient-specific parameters that are required to |
||
35 | * render the notifications-template or one of the notification-item-templates that |
||
36 | * are rendered into the notifications-template |
||
37 | * |
||
38 | * @param RecipientInterface $recipient |
||
39 | * @return array |
||
40 | */ |
||
41 | 2 | protected function getRecipientVarsForNotificationsEmail(RecipientInterface $recipient) |
|
49 | |||
50 | /** |
||
51 | * Get the subject for the notifications-email to send. Override this function to implement your custom subject-lines. |
||
52 | * @param array of array $contentItems |
||
53 | * @param RecipientInterface $recipient |
||
54 | * @return string |
||
55 | */ |
||
56 | 2 | public function getRecipientSpecificNotificationsSubject($contentItems, RecipientInterface $recipient) |
|
69 | |||
70 | /** |
||
71 | * Override this function to fill in any non-recipient-specific parameters that are required |
||
72 | * to render the newsletter-template and are not provided by the TemplateProvider. e.g. the total number of recipients of this newsletter |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | 1 | protected function getGeneralVarsForNewsletter() |
|
83 | |||
84 | /** |
||
85 | * Override this function to fill in any non-recipient-specific content items that are the same |
||
86 | * for all recipients of the newsletter. |
||
87 | * |
||
88 | * E.g. a list of featured events or news-articles. |
||
89 | * |
||
90 | * @return array of templatesIds (without ending) as key and params to render the template as value. => array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)); |
||
91 | */ |
||
92 | protected function getNonRecipientSpecificNewsletterContentItems() |
||
101 | |||
102 | /** |
||
103 | * Override this function to add more parameters that are required to render the newsletter template. |
||
104 | * @param RecipientInterface $recipient |
||
105 | * @return array |
||
106 | */ |
||
107 | 2 | public function getRecipientSpecificNewsletterParams(RecipientInterface $recipient) |
|
111 | |||
112 | /** |
||
113 | * Override this function to fill in any recipient-specific content items that are different |
||
114 | * depending on the recipient of the newsletter. |
||
115 | * |
||
116 | * E.g. a list of the recipients latest activites. |
||
117 | * |
||
118 | * @param RecipientInterface $recipient |
||
119 | * @return array of arrays with templatesIds (without ending) as key and params to render the template as value. |
||
120 | * => array( |
||
121 | * array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification1, 'goToUrl' => 'http://example.com/1', ...)) |
||
122 | * array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification2, 'goToUrl' => 'http://example.com/2', ...)) |
||
123 | * ); |
||
124 | */ |
||
125 | protected function getRecipientSpecificNewsletterContentItems(RecipientInterface $recipient) |
||
135 | |||
136 | /** |
||
137 | * Override this function to use a custom subject line for each newsletter-recipient. |
||
138 | * |
||
139 | * @param $generalContentItems array of content items. => e.g. array of array('templateID' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)) |
||
140 | * @param $recipientContentItems array of content items. => e.g. array of array('templateID' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)) |
||
141 | * @param $params array the array with all general template-params, including the item with the key 'subject' containing the default-subject |
||
142 | * @param $recipient RecipientInterface |
||
143 | * @param $locale string The language-code for translation of the subject |
||
144 | * @return the subject line |
||
145 | */ |
||
146 | 1 | public function getRecipientSpecificNewsletterSubject(array $generalContentItems, array $recipientContentItems, array $params, RecipientInterface $recipient, $locale) |
|
150 | |||
151 | /** |
||
152 | * By overriding this function you can rearrange the content items to you liking. By default no ordering is done, so the order is as follows: |
||
153 | * |
||
154 | * - all user-specific content items as returned by AzineNotifierService::getRecipientSpecificNewsletterContentItems |
||
155 | * - all non-user-specific content items as returned by AzineNotifierService::getNonRecipientSpecificNewsletterContentItems |
||
156 | * |
||
157 | * @param array $contentItems |
||
158 | * @return array |
||
159 | */ |
||
160 | 2 | public function orderContentItems(array $contentItems){ |
|
163 | |||
164 | /** |
||
165 | * Over ride this constructor if you need to inject more dependencies to get all the data together that you need for your newsletter/notifications. |
||
166 | * |
||
167 | * @param TemplateTwigSwiftMailerInterface $mailer |
||
168 | * @param \Twig_Environment $twig |
||
169 | * @param Logger $logger |
||
170 | * @param UrlGeneratorInterface $router |
||
171 | * @param ManagerRegistry $managerRegistry |
||
172 | * @param TemplateProviderInterface $templateProvider |
||
173 | * @param RecipientProviderInterface $recipientProvider |
||
174 | * @param Translator $translatorService |
||
175 | * @param array $parameters |
||
176 | */ |
||
177 | 8 | public function __construct(TemplateTwigSwiftMailerInterface $mailer, \Twig_Environment $twig, Logger $logger, UrlGeneratorInterface $router, |
|
191 | |||
192 | ////////////////////////////////////////////////////////////////////////// |
||
193 | /* You probably don't need to change or override any of the stuff below */ |
||
194 | ////////////////////////////////////////////////////////////////////////// |
||
195 | |||
196 | const CONTENT_ITEMS = 'contentItems'; |
||
197 | /** |
||
198 | * @var TemplateTwigSwiftMailerInterface |
||
199 | */ |
||
200 | protected $mailer; |
||
201 | |||
202 | /** |
||
203 | * @var \Twig_Environment |
||
204 | */ |
||
205 | protected $twig; |
||
206 | |||
207 | /** |
||
208 | * @var Logger |
||
209 | */ |
||
210 | protected $logger; |
||
211 | |||
212 | /** |
||
213 | * @var UrlGeneratorInterface |
||
214 | */ |
||
215 | protected $router; |
||
216 | |||
217 | /** |
||
218 | * @var TemplateProviderInterface |
||
219 | */ |
||
220 | protected $templateProvider; |
||
221 | |||
222 | /** |
||
223 | * @var RecipientProviderInterface |
||
224 | */ |
||
225 | protected $recipientProvider; |
||
226 | |||
227 | /** |
||
228 | * @var ManagerRegistry |
||
229 | */ |
||
230 | protected $managerRegistry; |
||
231 | |||
232 | /** |
||
233 | * Array of configuration-parameters from the config.yml |
||
234 | * @var array |
||
235 | */ |
||
236 | protected $configParameter; |
||
237 | |||
238 | /** |
||
239 | * The translator |
||
240 | * @var Translator |
||
241 | */ |
||
242 | protected $translatorService; |
||
243 | |||
244 | /** |
||
245 | * Get the number of seconds in a "one-hour-interval" |
||
246 | * @return integer of seconds to consider as an hour. |
||
247 | */ |
||
248 | 4 | protected function getHourInterval() |
|
256 | |||
257 | /** |
||
258 | * Get the number of seconds in a "one-day-interval" |
||
259 | * @return integer of seconds to consider as a day. |
||
260 | */ |
||
261 | 3 | protected function getDayInterval() |
|
269 | |||
270 | /** |
||
271 | * (non-PHPdoc) |
||
272 | * @see Azine\EmailBundle\Services.NotifierServiceInterface::sendNotifications() |
||
273 | */ |
||
274 | 3 | public function sendNotifications(array &$failedAddresses) |
|
303 | |||
304 | /** |
||
305 | * Send the notifications-email for one recipient |
||
306 | * @param integer $recipientId |
||
307 | * @param string $wrapperTemplateName |
||
308 | * @param array $params array of parameters for this recipient |
||
309 | * @return null|string or the failed email addressess |
||
310 | */ |
||
311 | 3 | public function sendNotificationsFor($recipientId, $wrapperTemplateName, $params) |
|
364 | |||
365 | /** |
||
366 | * (non-PHPdoc) |
||
367 | * @see Azine\EmailBundle\Services.NotifierServiceInterface::sendNewsletter() |
||
368 | */ |
||
369 | 2 | public function sendNewsletter(array &$failedAddresses) |
|
395 | |||
396 | /** |
||
397 | * Send the newsletter for one recipient |
||
398 | * @param integer $recipientId |
||
399 | * @param array $params params and contentItems that are the same for all recipients |
||
400 | * @param string $wrapperTemplate |
||
401 | * @return string|null or the failed email addressess |
||
402 | */ |
||
403 | 2 | public function sendNewsletterFor($recipientId, array $params, $wrapperTemplate) |
|
439 | |||
440 | /** |
||
441 | * Get the Notifications that have not yet been sent yet. |
||
442 | * Ordered by "template" and "title". |
||
443 | * @param RecipientInterface $recipient |
||
444 | * @return array of Notification |
||
445 | */ |
||
446 | 3 | protected function getNotificationsFor(RecipientInterface $recipient) |
|
483 | |||
484 | /** |
||
485 | * Get all IDs for Recipients of pending notifications |
||
486 | * @return array of IDs |
||
487 | */ |
||
488 | 3 | protected function getNotificationRecipientIds() |
|
492 | |||
493 | /** |
||
494 | * Update (set sent = now) and save the notifications |
||
495 | * @param array $notifications |
||
496 | */ |
||
497 | 2 | protected function setNotificationsAsSent(array $notifications) |
|
505 | |||
506 | /** |
||
507 | * Mark all Notifications as sent long ago, as the recipient never want's to get any notifications. |
||
508 | * @param RecipientInterface $recipient |
||
509 | */ |
||
510 | 1 | protected function markAllNotificationsAsSentFarInThePast(RecipientInterface $recipient) |
|
514 | |||
515 | /** |
||
516 | * Get the interval in days between newsletter mailings |
||
517 | */ |
||
518 | 1 | protected function getNewsletterInterval() |
|
522 | |||
523 | /** |
||
524 | * Get the time of the day when the newsletter should be sent. |
||
525 | * @return string Time of the day in the format HH:mm |
||
526 | */ |
||
527 | 1 | protected function getNewsletterSendTime() |
|
531 | |||
532 | /** |
||
533 | * Get the DateTime at which the last newsletter mailing probably has taken place, if a newsletter is sent today. |
||
534 | * (Calculated: send-time-today - interval in days) |
||
535 | * @return \DateTime |
||
536 | */ |
||
537 | 1 | protected function getDateTimeOfLastNewsletter() |
|
541 | |||
542 | /** |
||
543 | * Get the DateTime at which the next newsletter mailing will take place, if a newsletter is sent today. |
||
544 | * (Calculated: send-time-today + interval in days) |
||
545 | */ |
||
546 | 1 | protected function getDateTimeOfNextNewsletter() |
|
550 | |||
551 | /** |
||
552 | * Convenience-function to add and save a Notification-entity |
||
553 | * |
||
554 | * @param integer $recipientId the ID of the recipient of this notification => see RecipientProvider.getRecipient($id) |
||
555 | * @param string $title the title of the notification. depending on the recipients settings, multiple notifications are sent in one email. |
||
556 | * @param string $content the content of the notification |
||
557 | * @param string $template the twig-template to render the notification with |
||
558 | * @param array $templateVars the parameters used in the twig-template, 'notification' => Notification and 'recipient' => RecipientInterface will be added to this array when rendering the twig-template. |
||
559 | * @param integer $importance important messages are at the top of the notification-emails, un-important at the bottom. |
||
560 | * @param boolean $sendImmediately whether or not to ignore the recipients mailing-preference and send the notification a.s.a.p. |
||
561 | * @return Notification |
||
562 | */ |
||
563 | 2 | public function addNotification($recipientId, $title, $content, $template, $templateVars, $importance, $sendImmediately) |
|
578 | |||
579 | /** |
||
580 | * Convenience-function to add and save a Notification-entity for a message => see AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TYPE |
||
581 | * |
||
582 | * The following default are used: |
||
583 | * $importance = NORMAL |
||
584 | * $sendImmediately = fale |
||
585 | * $template = template for type AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TYPE |
||
586 | * $templateVars = only those from the template-provider |
||
587 | * |
||
588 | * @param integer $recipientId |
||
589 | * @param string $title |
||
590 | * @param string $content nl2br will be applied in the html-version of the email |
||
591 | * @param string $goToUrl if this is supplied, a link "Go to message" will be added. |
||
592 | */ |
||
593 | 1 | public function addNotificationMessage($recipientId, $title, $content, $goToUrl = null) |
|
602 | |||
603 | |||
604 | /** |
||
605 | * @return NotificationRepository |
||
606 | */ |
||
607 | 4 | protected function getNotificationRepository(){ |
|
610 | |||
611 | } |
||
612 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.