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 |
||
| 15 | class AzineNotifierService implements NotifierServiceInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Override this function to fill in any non-recipient-specific parameters that are required to |
||
| 19 | * render the notifications-template or one of the notification-item-templates that |
||
| 20 | * are rendered into the notifications-template |
||
| 21 | * |
||
| 22 | * @return array |
||
| 23 | */ |
||
| 24 | 3 | protected function getVarsForNotificationsEmail() |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Override this function to fill in any recipient-specific parameters that are required to |
||
| 33 | * render the notifications-template or one of the notification-item-templates that |
||
| 34 | * are rendered into the notifications-template |
||
| 35 | * |
||
| 36 | * @param RecipientInterface $recipient |
||
| 37 | * @return array |
||
| 38 | */ |
||
| 39 | 2 | protected function getRecipientVarsForNotificationsEmail(RecipientInterface $recipient) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Get the subject for the notifications-email to send. Override this function to implement your custom subject-lines. |
||
| 50 | * @param array of array $contentItems |
||
| 51 | * @param RecipientInterface $recipient |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 2 | public function getRecipientSpecificNotificationsSubject($contentItems, RecipientInterface $recipient) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Override this function to fill in any non-recipient-specific parameters that are required |
||
| 70 | * to render the newsletter-template and are not provided by the TemplateProvider. e.g. the total number of recipients of this newsletter |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | 1 | protected function getGeneralVarsForNewsletter() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Override this function to fill in any non-recipient-specific content items that are the same |
||
| 84 | * for all recipients of the newsletter. |
||
| 85 | * |
||
| 86 | * E.g. a list of featured events or news-articles. |
||
| 87 | * |
||
| 88 | * @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', ...)); |
||
| 89 | */ |
||
| 90 | protected function getNonRecipientSpecificNewsletterContentItems() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Override this function to add more parameters that are required to render the newsletter template. |
||
| 102 | * @param RecipientInterface $recipient |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | 2 | public function getRecipientSpecificNewsletterParams(RecipientInterface $recipient) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Override this function to fill in any recipient-specific content items that are different |
||
| 112 | * depending on the recipient of the newsletter. |
||
| 113 | * |
||
| 114 | * E.g. a list of the recipients latest activites. |
||
| 115 | * |
||
| 116 | * @param RecipientInterface $recipient |
||
| 117 | * @return array of arrays with templatesIds (without ending) as key and params to render the template as value. |
||
| 118 | * => array( |
||
| 119 | * array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification1, 'goToUrl' => 'http://example.com/1', ...)) |
||
| 120 | * array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification2, 'goToUrl' => 'http://example.com/2', ...)) |
||
| 121 | * ); |
||
| 122 | */ |
||
| 123 | protected function getRecipientSpecificNewsletterContentItems(RecipientInterface $recipient) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Override this function to use a custom subject line for each newsletter-recipient. |
||
| 136 | * |
||
| 137 | * @param $generalContentItems array of content items. => e.g. array of array('templateID' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)) |
||
| 138 | * @param $recipientContentItems array of content items. => e.g. array of array('templateID' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)) |
||
| 139 | * @param $params array the array with all general template-params, including the item with the key 'subject' containing the default-subject |
||
| 140 | * @param $recipient RecipientInterface |
||
| 141 | * @param $locale string The language-code for translation of the subject |
||
| 142 | * @return the subject line |
||
| 143 | */ |
||
| 144 | 1 | public function getRecipientSpecificNewsletterSubject(array $generalContentItems, array $recipientContentItems, array $params, RecipientInterface $recipient, $locale) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * 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: |
||
| 151 | * |
||
| 152 | * - all user-specific content items as returned by AzineNotifierService::getRecipientSpecificNewsletterContentItems |
||
| 153 | * - all non-user-specific content items as returned by AzineNotifierService::getNonRecipientSpecificNewsletterContentItems |
||
| 154 | * |
||
| 155 | * @param array $contentItems |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 2 | public function orderContentItems(array $contentItems){ |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Over ride this constructor if you need to inject more dependencies to get all the data together that you need for your newsletter/notifications. |
||
| 164 | * |
||
| 165 | * @param TemplateTwigSwiftMailerInterface $mailer |
||
| 166 | * @param \Twig_Environment $twig |
||
| 167 | * @param UrlGeneratorInterface $router |
||
| 168 | * @param ManagerRegistry $managerRegistry |
||
| 169 | * @param TemplateProviderInterface $templateProvider |
||
| 170 | * @param RecipientProviderInterface $recipientProvider |
||
| 171 | * @param Translator $translatorService |
||
| 172 | * @param array $parameters |
||
| 173 | */ |
||
| 174 | 8 | public function __construct(TemplateTwigSwiftMailerInterface $mailer, \Twig_Environment $twig, UrlGeneratorInterface $router, |
|
| 187 | |||
| 188 | ////////////////////////////////////////////////////////////////////////// |
||
| 189 | /* You probably don't need to change or override any of the stuff below */ |
||
| 190 | ////////////////////////////////////////////////////////////////////////// |
||
| 191 | |||
| 192 | const CONTENT_ITEMS = 'contentItems'; |
||
| 193 | /** |
||
| 194 | * @var TemplateTwigSwiftMailerInterface |
||
| 195 | */ |
||
| 196 | protected $mailer; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @var \Twig_Environment |
||
| 200 | */ |
||
| 201 | protected $twig; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @var UrlGeneratorInterface |
||
| 205 | */ |
||
| 206 | protected $router; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var TemplateProviderInterface |
||
| 210 | */ |
||
| 211 | protected $templateProvider; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @var RecipientProviderInterface |
||
| 215 | */ |
||
| 216 | protected $recipientProvider; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @var ManagerRegistry |
||
| 220 | */ |
||
| 221 | protected $managerRegistry; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Array of configuration-parameters from the config.yml |
||
| 225 | * @var array |
||
| 226 | */ |
||
| 227 | protected $configParameter; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * The translator |
||
| 231 | * @var Translator |
||
| 232 | */ |
||
| 233 | protected $translatorService; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the number of seconds in a "one-hour-interval" |
||
| 237 | * @return integer of seconds to consider as an hour. |
||
| 238 | */ |
||
| 239 | 4 | protected function getHourInterval() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Get the number of seconds in a "one-day-interval" |
||
| 250 | * @return integer of seconds to consider as a day. |
||
| 251 | */ |
||
| 252 | 3 | protected function getDayInterval() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * (non-PHPdoc) |
||
| 263 | * @see Azine\EmailBundle\Services.NotifierServiceInterface::sendNotifications() |
||
| 264 | */ |
||
| 265 | 3 | public function sendNotifications(array &$failedAddresses) |
|
| 266 | { |
||
| 267 | // get all recipientIds with pending notifications in the database, that are due to be sent |
||
| 268 | 3 | $recipientIds = $this->getNotificationRecipientIds(); |
|
| 269 | |||
| 270 | // get vars that are the same for all recipients of this notification-mail-batch |
||
| 271 | 3 | $params = $this->getVarsForNotificationsEmail(); |
|
| 272 | |||
| 273 | 3 | $notificationsTemplate = $this->configParameter[AzineEmailExtension::TEMPLATES."_".AzineEmailExtension::NOTIFICATIONS_TEMPLATE]; |
|
| 274 | |||
| 275 | 3 | $sentCount = 0; |
|
| 276 | 3 | View Code Duplication | foreach ($recipientIds as $recipientId) { |
| 277 | |||
| 278 | // send the mail for this recipient |
||
| 279 | 3 | $failedAddress = $this->sendNotificationsFor($recipientId, $notificationsTemplate, $params); |
|
| 280 | 3 | if ($failedAddress !== null && strlen($failedAddress) > 0) { |
|
| 281 | 2 | $failedAddresses[] = $failedAddress; |
|
| 282 | 2 | } else { |
|
| 283 | 3 | $sentCount++; |
|
| 284 | } |
||
| 285 | 3 | } |
|
| 286 | |||
| 287 | 3 | return $sentCount; |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Send the notifications-email for one recipient |
||
| 292 | * @param integer $recipientId |
||
| 293 | * @param string $wrapperTemplateName |
||
| 294 | * @param array $params array of parameters for this recipient |
||
| 295 | * @return null|string or the failed email addressess |
||
| 296 | */ |
||
| 297 | 3 | public function sendNotificationsFor($recipientId, $wrapperTemplateName, $params) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * (non-PHPdoc) |
||
| 349 | * @see Azine\EmailBundle\Services.NotifierServiceInterface::sendNewsletter() |
||
| 350 | */ |
||
| 351 | 2 | public function sendNewsletter(array &$failedAddresses) |
|
| 352 | { |
||
| 353 | // params array for all recipients |
||
| 354 | 2 | $params = array(); |
|
| 355 | |||
| 356 | // set a default subject |
||
| 357 | 2 | $params['subject'] = $this->translatorService->trans("_az.email.newsletter.subject"); |
|
| 358 | |||
| 359 | // get the the non-recipient-specific contentItems of the newsletter |
||
| 360 | 2 | $params[self::CONTENT_ITEMS] = $this->getNonRecipientSpecificNewsletterContentItems(); |
|
| 361 | |||
| 362 | // get recipientIds for the newsletter |
||
| 363 | 2 | $recipientIds = $this->recipientProvider->getNewsletterRecipientIDs(); |
|
| 364 | |||
| 365 | 2 | $newsletterTemplate = $this->configParameter[AzineEmailExtension::TEMPLATES."_".AzineEmailExtension::NEWSLETTER_TEMPLATE]; |
|
| 366 | |||
| 367 | 2 | View Code Duplication | foreach ($recipientIds as $recipientId) { |
| 368 | 2 | $failedAddress = $this->sendNewsletterFor($recipientId, $params, $newsletterTemplate); |
|
| 369 | |||
| 370 | 2 | if ($failedAddress !== null && strlen($failedAddress) > 0) { |
|
| 371 | 2 | $failedAddresses[] = $failedAddress; |
|
| 372 | 2 | } |
|
| 373 | 2 | } |
|
| 374 | |||
| 375 | 2 | return sizeof($recipientIds) - sizeof($failedAddresses); |
|
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Send the newsletter for one recipient |
||
| 380 | * @param integer $recipientId |
||
| 381 | * @param array $params params and contentItems that are the same for all recipients |
||
| 382 | * @param string $wrapperTemplate |
||
| 383 | * @return string|null or the failed email addressess |
||
| 384 | */ |
||
| 385 | 2 | public function sendNewsletterFor($recipientId, array $params, $wrapperTemplate) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Get the Notifications that have not yet been sent yet. |
||
| 420 | * Ordered by "template" and "title". |
||
| 421 | * @param RecipientInterface $recipient |
||
| 422 | * @return array of Notification |
||
| 423 | */ |
||
| 424 | 3 | protected function getNotificationsFor(RecipientInterface $recipient) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Get all IDs for Recipients of pending notifications |
||
| 464 | * @return array of IDs |
||
| 465 | */ |
||
| 466 | 3 | protected function getNotificationRecipientIds() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Update (set sent = now) and save the notifications |
||
| 473 | * @param array $notifications |
||
| 474 | */ |
||
| 475 | 2 | protected function setNotificationsAsSent(array $notifications) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Mark all Notifications as sent long ago, as the recipient never want's to get any notifications. |
||
| 486 | * @param RecipientInterface $recipient |
||
| 487 | */ |
||
| 488 | 1 | protected function markAllNotificationsAsSentFarInThePast(RecipientInterface $recipient) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Get the interval in days between newsletter mailings |
||
| 495 | */ |
||
| 496 | 1 | protected function getNewsletterInterval() |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Get the time of the day when the newsletter should be sent. |
||
| 503 | * @return string Time of the day in the format HH:mm |
||
| 504 | */ |
||
| 505 | 1 | protected function getNewsletterSendTime() |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Get the DateTime at which the last newsletter mailing probably has taken place, if a newsletter is sent today. |
||
| 512 | * (Calculated: send-time-today - interval in days) |
||
| 513 | * @return \DateTime |
||
| 514 | */ |
||
| 515 | 1 | protected function getDateTimeOfLastNewsletter() |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Get the DateTime at which the next newsletter mailing will take place, if a newsletter is sent today. |
||
| 522 | * (Calculated: send-time-today + interval in days) |
||
| 523 | */ |
||
| 524 | 1 | protected function getDateTimeOfNextNewsletter() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Convenience-function to add and save a Notification-entity |
||
| 531 | * |
||
| 532 | * @param integer $recipientId the ID of the recipient of this notification => see RecipientProvider.getRecipient($id) |
||
| 533 | * @param string $title the title of the notification. depending on the recipients settings, multiple notifications are sent in one email. |
||
| 534 | * @param string $content the content of the notification |
||
| 535 | * @param string $template the twig-template to render the notification with |
||
| 536 | * @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. |
||
| 537 | * @param integer $importance important messages are at the top of the notification-emails, un-important at the bottom. |
||
| 538 | * @param boolean $sendImmediately whether or not to ignore the recipients mailing-preference and send the notification a.s.a.p. |
||
| 539 | * @return Notification |
||
| 540 | */ |
||
| 541 | 2 | public function addNotification($recipientId, $title, $content, $template, $templateVars, $importance, $sendImmediately) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Convenience-function to add and save a Notification-entity for a message => see AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TYPE |
||
| 559 | * |
||
| 560 | * The following default are used: |
||
| 561 | * $importance = NORMAL |
||
| 562 | * $sendImmediately = fale |
||
| 563 | * $template = template for type AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TYPE |
||
| 564 | * $templateVars = only those from the template-provider |
||
| 565 | * |
||
| 566 | * @param integer $recipientId |
||
| 567 | * @param string $title |
||
| 568 | * @param string $content nl2br will be applied in the html-version of the email |
||
| 569 | * @param string $goToUrl if this is supplied, a link "Go to message" will be added. |
||
| 570 | */ |
||
| 571 | 1 | public function addNotificationMessage($recipientId, $title, $content, $goToUrl = null) |
|
| 580 | |||
| 581 | |||
| 582 | /** |
||
| 583 | * @return NotificationRepository |
||
| 584 | */ |
||
| 585 | 4 | protected function getNotificationRepository(){ |
|
| 588 | |||
| 589 | } |
||
| 590 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.