azine /
email-bundle
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Azine\EmailBundle\Services; |
||||||
| 4 | |||||||
| 5 | use Azine\EmailBundle\DependencyInjection\AzineEmailExtension; |
||||||
| 6 | use Azine\EmailBundle\Entity\Notification; |
||||||
| 7 | use Azine\EmailBundle\Entity\RecipientInterface; |
||||||
| 8 | use Azine\EmailBundle\Entity\Repositories\NotificationRepository; |
||||||
| 9 | use Doctrine\Common\Persistence\ManagerRegistry; |
||||||
| 10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||||||
| 11 | use Symfony\Component\Translation\TranslatorInterface; |
||||||
| 12 | |||||||
| 13 | /** |
||||||
| 14 | * This Service compiles and renders the emails to be sent. |
||||||
| 15 | * |
||||||
| 16 | * @author Dominik Businger |
||||||
| 17 | */ |
||||||
| 18 | class AzineNotifierService implements NotifierServiceInterface |
||||||
| 19 | { |
||||||
| 20 | /** |
||||||
| 21 | * Override this function to fill in any non-recipient-specific parameters that are required to |
||||||
| 22 | * render the notifications-template or one of the notification-item-templates that |
||||||
| 23 | * are rendered into the notifications-template. |
||||||
| 24 | * |
||||||
| 25 | * @return array |
||||||
| 26 | */ |
||||||
| 27 | 3 | protected function getVarsForNotificationsEmail() |
|||||
| 28 | { |
||||||
| 29 | 3 | $params = array(); |
|||||
| 30 | |||||||
| 31 | 3 | return $params; |
|||||
| 32 | } |
||||||
| 33 | |||||||
| 34 | /** |
||||||
| 35 | * Override this function to fill in any recipient-specific parameters that are required to |
||||||
| 36 | * render the notifications-template or one of the notification-item-templates that |
||||||
| 37 | * are rendered into the notifications-template. |
||||||
| 38 | * |
||||||
| 39 | * @param RecipientInterface $recipient |
||||||
| 40 | * |
||||||
| 41 | * @return array |
||||||
| 42 | */ |
||||||
| 43 | 2 | protected function getRecipientVarsForNotificationsEmail(RecipientInterface $recipient) |
|||||
| 44 | { |
||||||
| 45 | 2 | $recipientParams = array(); |
|||||
| 46 | 2 | $recipientParams['recipient'] = $recipient; |
|||||
| 47 | 2 | $recipientParams['mode'] = $recipient->getNotificationMode(); |
|||||
| 48 | |||||||
| 49 | 2 | return $recipientParams; |
|||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | /** |
||||||
| 53 | * Get the subject for the notifications-email to send. Override this function to implement your custom subject-lines. |
||||||
| 54 | * |
||||||
| 55 | * @param array of array $contentItems |
||||||
| 56 | * @param RecipientInterface $recipient |
||||||
| 57 | * |
||||||
| 58 | * @return string |
||||||
| 59 | */ |
||||||
| 60 | 2 | public function getRecipientSpecificNotificationsSubject($contentItems, RecipientInterface $recipient) |
|||||
| 61 | { |
||||||
| 62 | 2 | $count = sizeof($contentItems); |
|||||
| 63 | |||||||
| 64 | 2 | if (1 == $count) { |
|||||
| 65 | // get the content-item out of the boxed associative array => array(array('templateId' => contentItem)) |
||||||
| 66 | 2 | $onlyItem = current(current($contentItems)); |
|||||
| 67 | // get the title out of the notification in the contentItem |
||||||
| 68 | 2 | return $onlyItem['notification']->getTitle(); |
|||||
| 69 | } |
||||||
| 70 | |||||||
| 71 | return $this->translatorService->transChoice('_az.email.notifications.subject.%count%', $count, array('%count%' => $count)); |
||||||
| 72 | } |
||||||
| 73 | |||||||
| 74 | /** |
||||||
| 75 | * Override this function to fill in any non-recipient-specific parameters that are required |
||||||
| 76 | * to render the newsletter-template and are not provided by the TemplateProvider. e.g. the total number of recipients of this newsletter. |
||||||
| 77 | * |
||||||
| 78 | * @return array |
||||||
| 79 | */ |
||||||
| 80 | 1 | protected function getGeneralVarsForNewsletter() |
|||||
| 81 | { |
||||||
| 82 | 1 | $vars = array(); |
|||||
| 83 | 1 | $vars['recipientCount'] = sizeof($this->recipientProvider->getNewsletterRecipientIDs()); |
|||||
| 84 | |||||||
| 85 | 1 | return $vars; |
|||||
| 86 | } |
||||||
| 87 | |||||||
| 88 | /** |
||||||
| 89 | * Override this function to fill in any non-recipient-specific content items that are the same |
||||||
| 90 | * for all recipients of the newsletter. |
||||||
| 91 | * |
||||||
| 92 | * E.g. a list of featured events or news-articles. |
||||||
| 93 | * |
||||||
| 94 | * @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', ...)); |
||||||
| 95 | */ |
||||||
| 96 | protected function getNonRecipientSpecificNewsletterContentItems() |
||||||
| 97 | { |
||||||
| 98 | // @codeCoverageIgnoreStart |
||||||
| 99 | $contentItems = array(); |
||||||
| 100 | |||||||
| 101 | //$contentItems[] = array('AcmeBundle:foo:barSameForAllRecipientsTemplate' => $templateParams); |
||||||
| 102 | return $contentItems; |
||||||
| 103 | // @codeCoverageIgnoreEnd |
||||||
| 104 | } |
||||||
| 105 | |||||||
| 106 | /** |
||||||
| 107 | * Override this function to add more parameters that are required to render the newsletter template. |
||||||
| 108 | * |
||||||
| 109 | * @param RecipientInterface $recipient |
||||||
| 110 | * |
||||||
| 111 | * @return array |
||||||
| 112 | */ |
||||||
| 113 | 2 | public function getRecipientSpecificNewsletterParams(RecipientInterface $recipient) |
|||||
| 114 | { |
||||||
| 115 | 2 | return array('recipient' => $recipient); |
|||||
| 116 | } |
||||||
| 117 | |||||||
| 118 | /** |
||||||
| 119 | * Override this function to fill in any recipient-specific content items that are different |
||||||
| 120 | * depending on the recipient of the newsletter. |
||||||
| 121 | * |
||||||
| 122 | * E.g. a list of the recipients latest activites. |
||||||
| 123 | * |
||||||
| 124 | * @param RecipientInterface $recipient |
||||||
| 125 | * |
||||||
| 126 | * @return array of arrays with templatesIds (without ending) as key and params to render the template as value. |
||||||
| 127 | * => array( |
||||||
| 128 | * array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification1, 'goToUrl' => 'http://example.com/1', ...)) |
||||||
| 129 | * array('AzineEmailBundle:contentItem:message' => array('notification => $someNotification2, 'goToUrl' => 'http://example.com/2', ...)) |
||||||
| 130 | * ); |
||||||
| 131 | */ |
||||||
| 132 | protected function getRecipientSpecificNewsletterContentItems(RecipientInterface $recipient) |
||||||
| 133 | { |
||||||
| 134 | // @codeCoverageIgnoreStart |
||||||
| 135 | $contentItems = array(); |
||||||
| 136 | |||||||
| 137 | //$contentItems[] = array('AcmeBundle:foo:barDifferentForEachRecipientTemplate' => $recipientSpecificTemplateParams); |
||||||
| 138 | //$contentItems[] = array(AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TEMPLATE => array('notification' => array('title' => 'SampleMessage', 'created' => new \DateTime('1 hour ago'), 'content' => 'Sample Text. Lorem Ipsum.'))); |
||||||
| 139 | return $contentItems; |
||||||
| 140 | // @codeCoverageIgnoreEnd |
||||||
| 141 | } |
||||||
| 142 | |||||||
| 143 | /** |
||||||
| 144 | * Override this function to use a custom subject line for each newsletter-recipient. |
||||||
| 145 | * |
||||||
| 146 | * @param $generalContentItems array of content items. => e.g. array of array('templateID' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)) |
||||||
| 147 | * @param $recipientContentItems array of content items. => e.g. array of array('templateID' => array('notification => $someNotification, 'goToUrl' => 'http://example.com', ...)) |
||||||
| 148 | * @param $params array the array with all general template-params, including the item with the key 'subject' containing the default-subject |
||||||
| 149 | * @param $recipient RecipientInterface |
||||||
| 150 | * @param $locale string The language-code for translation of the subject |
||||||
| 151 | * |
||||||
| 152 | * @return the subject line |
||||||
| 153 | */ |
||||||
| 154 | 1 | public function getRecipientSpecificNewsletterSubject(array $generalContentItems, array $recipientContentItems, array $params, RecipientInterface $recipient, $locale) |
|||||
|
0 ignored issues
–
show
The parameter
$locale is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$recipientContentItems is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 155 | { |
||||||
| 156 | 1 | return $params['subject']; |
|||||
| 157 | } |
||||||
| 158 | |||||||
| 159 | /** |
||||||
| 160 | * 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:. |
||||||
| 161 | * |
||||||
| 162 | * - all user-specific content items as returned by AzineNotifierService::getRecipientSpecificNewsletterContentItems |
||||||
| 163 | * - all non-user-specific content items as returned by AzineNotifierService::getNonRecipientSpecificNewsletterContentItems |
||||||
| 164 | * |
||||||
| 165 | * @param array $contentItems |
||||||
| 166 | * |
||||||
| 167 | * @return array |
||||||
| 168 | */ |
||||||
| 169 | 2 | public function orderContentItems(array $contentItems) |
|||||
| 170 | { |
||||||
| 171 | 2 | return $contentItems; |
|||||
| 172 | } |
||||||
| 173 | |||||||
| 174 | /** |
||||||
| 175 | * Over ride this constructor if you need to inject more dependencies to get all the data together that you need for your newsletter/notifications. |
||||||
| 176 | * |
||||||
| 177 | * @param TemplateTwigSwiftMailerInterface $mailer |
||||||
| 178 | * @param \Twig_Environment $twig |
||||||
| 179 | * @param UrlGeneratorInterface $router |
||||||
| 180 | * @param ManagerRegistry $managerRegistry |
||||||
| 181 | * @param TemplateProviderInterface $templateProvider |
||||||
| 182 | * @param RecipientProviderInterface $recipientProvider |
||||||
| 183 | * @param TranslatorInterface $translatorService |
||||||
| 184 | * @param array $parameters |
||||||
| 185 | */ |
||||||
| 186 | 8 | public function __construct(TemplateTwigSwiftMailerInterface $mailer, \Twig_Environment $twig, UrlGeneratorInterface $router, |
|||||
| 187 | ManagerRegistry $managerRegistry, TemplateProviderInterface $templateProvider, RecipientProviderInterface $recipientProvider, |
||||||
| 188 | TranslatorInterface $translatorService, array $parameters) |
||||||
| 189 | { |
||||||
| 190 | 8 | $this->mailer = $mailer; |
|||||
| 191 | 8 | $this->twig = $twig; |
|||||
| 192 | 8 | $this->router = $router; |
|||||
| 193 | 8 | $this->managerRegistry = $managerRegistry; |
|||||
| 194 | 8 | $this->templateProvider = $templateProvider; |
|||||
| 195 | 8 | $this->recipientProvider = $recipientProvider; |
|||||
| 196 | 8 | $this->translatorService = $translatorService; |
|||||
| 197 | 8 | $this->configParameter = $parameters; |
|||||
| 198 | 8 | } |
|||||
| 199 | |||||||
| 200 | ////////////////////////////////////////////////////////////////////////// |
||||||
| 201 | /* You probably don't need to change or override any of the stuff below */ |
||||||
| 202 | ////////////////////////////////////////////////////////////////////////// |
||||||
| 203 | |||||||
| 204 | const CONTENT_ITEMS = 'contentItems'; |
||||||
| 205 | /** |
||||||
| 206 | * @var TemplateTwigSwiftMailerInterface |
||||||
| 207 | */ |
||||||
| 208 | protected $mailer; |
||||||
| 209 | |||||||
| 210 | /** |
||||||
| 211 | * @var \Twig_Environment |
||||||
| 212 | */ |
||||||
| 213 | protected $twig; |
||||||
| 214 | |||||||
| 215 | /** |
||||||
| 216 | * @var UrlGeneratorInterface |
||||||
| 217 | */ |
||||||
| 218 | protected $router; |
||||||
| 219 | |||||||
| 220 | /** |
||||||
| 221 | * @var TemplateProviderInterface |
||||||
| 222 | */ |
||||||
| 223 | protected $templateProvider; |
||||||
| 224 | |||||||
| 225 | /** |
||||||
| 226 | * @var RecipientProviderInterface |
||||||
| 227 | */ |
||||||
| 228 | protected $recipientProvider; |
||||||
| 229 | |||||||
| 230 | /** |
||||||
| 231 | * @var ManagerRegistry |
||||||
| 232 | */ |
||||||
| 233 | protected $managerRegistry; |
||||||
| 234 | |||||||
| 235 | /** |
||||||
| 236 | * Array of configuration-parameters from the config.yml. |
||||||
| 237 | * |
||||||
| 238 | * @var array |
||||||
| 239 | */ |
||||||
| 240 | protected $configParameter; |
||||||
| 241 | |||||||
| 242 | /** |
||||||
| 243 | * The translator. |
||||||
| 244 | * |
||||||
| 245 | * @var TranslatorInterface |
||||||
| 246 | */ |
||||||
| 247 | protected $translatorService; |
||||||
| 248 | |||||||
| 249 | /** |
||||||
| 250 | * Get the number of seconds in a "one-hour-interval". |
||||||
| 251 | * |
||||||
| 252 | * @return int of seconds to consider as an hour |
||||||
| 253 | */ |
||||||
| 254 | 4 | protected function getHourInterval() |
|||||
| 255 | { |
||||||
| 256 | // about an hour ago (57min) |
||||||
| 257 | // this is because if the last run started 60min. ago, then the notifications |
||||||
| 258 | // for any recipient have been send after that and would be skipped until the next run. |
||||||
| 259 | // if your cron-job runs every minute, this is not needed. |
||||||
| 260 | 4 | return 60 * 60 - 3 * 60; |
|||||
| 261 | } |
||||||
| 262 | |||||||
| 263 | /** |
||||||
| 264 | * Get the number of seconds in a "one-day-interval". |
||||||
| 265 | * |
||||||
| 266 | * @return int of seconds to consider as a day |
||||||
| 267 | */ |
||||||
| 268 | 3 | protected function getDayInterval() |
|||||
| 269 | { |
||||||
| 270 | // about a day ago (23h57min) |
||||||
| 271 | // this is because if the last run started 24h. ago, then the notifications |
||||||
| 272 | // for any recipient have been send after that and would be skipped until the next run. |
||||||
| 273 | // if your cron-job runs every minute, this is not needed. |
||||||
| 274 | 3 | return 60 * 60 * 24 - 3 * 60; |
|||||
| 275 | } |
||||||
| 276 | |||||||
| 277 | /** |
||||||
| 278 | * (non-PHPdoc). |
||||||
| 279 | * |
||||||
| 280 | * @see Azine\EmailBundle\Services.NotifierServiceInterface::sendNotifications() |
||||||
| 281 | */ |
||||||
| 282 | 3 | public function sendNotifications(array &$failedAddresses) |
|||||
| 283 | { |
||||||
| 284 | // get all recipientIds with pending notifications in the database, that are due to be sent |
||||||
| 285 | 3 | $recipientIds = $this->getNotificationRecipientIds(); |
|||||
| 286 | |||||||
| 287 | // get vars that are the same for all recipients of this notification-mail-batch |
||||||
| 288 | 3 | $params = $this->getVarsForNotificationsEmail(); |
|||||
| 289 | |||||||
| 290 | 3 | $notificationsTemplate = $this->configParameter[AzineEmailExtension::TEMPLATES.'_'.AzineEmailExtension::NOTIFICATIONS_TEMPLATE]; |
|||||
| 291 | |||||||
| 292 | 3 | $sentCount = 0; |
|||||
| 293 | 3 | foreach ($recipientIds as $recipientId) { |
|||||
| 294 | // send the mail for this recipient |
||||||
| 295 | 3 | $failedAddress = $this->sendNotificationsFor($recipientId, $notificationsTemplate, $params); |
|||||
| 296 | 3 | if (null !== $failedAddress && strlen($failedAddress) > 0) { |
|||||
| 297 | 2 | $failedAddresses[] = $failedAddress; |
|||||
| 298 | } else { |
||||||
| 299 | 3 | ++$sentCount; |
|||||
| 300 | } |
||||||
| 301 | } |
||||||
| 302 | |||||||
| 303 | 3 | return $sentCount; |
|||||
| 304 | } |
||||||
| 305 | |||||||
| 306 | /** |
||||||
| 307 | * Send the notifications-email for one recipient. |
||||||
| 308 | * |
||||||
| 309 | * @param int $recipientId |
||||||
| 310 | * @param string $wrapperTemplateName |
||||||
| 311 | * @param array $params array of parameters for this recipient |
||||||
| 312 | * |
||||||
| 313 | * @return string|null or the failed email addressess |
||||||
| 314 | */ |
||||||
| 315 | 3 | public function sendNotificationsFor($recipientId, $wrapperTemplateName, $params) |
|||||
| 316 | { |
||||||
| 317 | // get the recipient |
||||||
| 318 | 3 | $recipient = $this->recipientProvider->getRecipient($recipientId); |
|||||
| 319 | |||||||
| 320 | // get all Notification-Items for the recipient from the database |
||||||
| 321 | 3 | $notifications = $this->getNotificationsFor($recipient); |
|||||
| 322 | 3 | if (0 == sizeof($notifications)) { |
|||||
| 323 | 1 | return null; |
|||||
| 324 | } |
||||||
| 325 | |||||||
| 326 | // get the recipient specific parameters for the twig-templates |
||||||
| 327 | 2 | $recipientParams = $this->getRecipientVarsForNotificationsEmail($recipient); |
|||||
| 328 | 2 | $params = array_merge($recipientParams, $params); |
|||||
| 329 | |||||||
| 330 | // prepare the arrays with template and template-variables for each notification |
||||||
| 331 | 2 | $contentItems = array(); |
|||||
| 332 | 2 | foreach ($notifications as $notification) { |
|||||
| 333 | // decode the $params from the json in the notification-entity |
||||||
| 334 | 2 | $itemVars = $notification->getVariables(); |
|||||
| 335 | 2 | $itemVars = array_merge($params, $itemVars); |
|||||
| 336 | 2 | $itemVars['notification'] = $notification; |
|||||
| 337 | 2 | $itemVars['recipient'] = $recipient; |
|||||
| 338 | |||||||
| 339 | 2 | $itemTemplateName = $notification->getTemplate(); |
|||||
| 340 | |||||||
| 341 | 2 | $contentItems[] = array($itemTemplateName => $itemVars); |
|||||
| 342 | } |
||||||
| 343 | |||||||
| 344 | // add the notifications to the params array so they will be rendered later |
||||||
| 345 | 2 | $params[self::CONTENT_ITEMS] = $contentItems; |
|||||
| 346 | 2 | $params['recipient'] = $recipient; |
|||||
| 347 | 2 | $params['_locale'] = $recipient->getPreferredLocale(); |
|||||
| 348 | |||||||
| 349 | 2 | $subject = $this->getRecipientSpecificNotificationsSubject($contentItems, $recipient); |
|||||
| 350 | |||||||
| 351 | // send the email with the right wrapper-template |
||||||
| 352 | 2 | $sent = $this->mailer->sendSingleEmail($recipient->getEmail(), $recipient->getDisplayName(), $subject, $params, $wrapperTemplateName.'.txt.twig', $recipient->getPreferredLocale()); |
|||||
| 353 | |||||||
| 354 | 2 | if ($sent) { |
|||||
| 355 | // save the updated notifications |
||||||
| 356 | 2 | $this->setNotificationsAsSent($notifications); |
|||||
| 357 | |||||||
| 358 | 2 | return null; |
|||||
| 359 | } |
||||||
| 360 | |||||||
| 361 | 2 | return $recipient->getEmail(); |
|||||
| 362 | } |
||||||
| 363 | |||||||
| 364 | /** |
||||||
| 365 | * (non-PHPdoc). |
||||||
| 366 | * |
||||||
| 367 | * @see Azine\EmailBundle\Services.NotifierServiceInterface::sendNewsletter() |
||||||
| 368 | */ |
||||||
| 369 | 2 | public function sendNewsletter(array &$failedAddresses) |
|||||
| 370 | { |
||||||
| 371 | // params array for all recipients |
||||||
| 372 | 2 | $params = array(); |
|||||
| 373 | |||||||
| 374 | // set a default subject |
||||||
| 375 | 2 | $params['subject'] = $this->translatorService->trans('_az.email.newsletter.subject'); |
|||||
| 376 | |||||||
| 377 | // get the the non-recipient-specific contentItems of the newsletter |
||||||
| 378 | 2 | $params[self::CONTENT_ITEMS] = $this->getNonRecipientSpecificNewsletterContentItems(); |
|||||
| 379 | |||||||
| 380 | // get recipientIds for the newsletter |
||||||
| 381 | 2 | $recipientIds = $this->recipientProvider->getNewsletterRecipientIDs(); |
|||||
| 382 | |||||||
| 383 | 2 | $newsletterTemplate = $this->configParameter[AzineEmailExtension::TEMPLATES.'_'.AzineEmailExtension::NEWSLETTER_TEMPLATE]; |
|||||
| 384 | |||||||
| 385 | 2 | foreach ($recipientIds as $recipientId) { |
|||||
| 386 | 2 | $failedAddress = $this->sendNewsletterFor($recipientId, $params, $newsletterTemplate); |
|||||
| 387 | |||||||
| 388 | 2 | if (null !== $failedAddress && strlen($failedAddress) > 0) { |
|||||
| 389 | 2 | $failedAddresses[] = $failedAddress; |
|||||
| 390 | } |
||||||
| 391 | } |
||||||
| 392 | |||||||
| 393 | 2 | return sizeof($recipientIds) - sizeof($failedAddresses); |
|||||
| 394 | } |
||||||
| 395 | |||||||
| 396 | /** |
||||||
| 397 | * Send the newsletter for one recipient. |
||||||
| 398 | * |
||||||
| 399 | * @param int $recipientId |
||||||
| 400 | * @param array $params params and contentItems that are the same for all recipients |
||||||
| 401 | * @param string $wrapperTemplate |
||||||
| 402 | * |
||||||
| 403 | * @return string|null or the failed email addressess |
||||||
| 404 | */ |
||||||
| 405 | 2 | public function sendNewsletterFor($recipientId, array $params, $wrapperTemplate) |
|||||
| 406 | { |
||||||
| 407 | 2 | $recipient = $this->recipientProvider->getRecipient($recipientId); |
|||||
| 408 | |||||||
| 409 | // create new array for each recipient. |
||||||
| 410 | 2 | $recipientParams = array_merge($params, $this->getRecipientSpecificNewsletterParams($recipient)); |
|||||
| 411 | |||||||
| 412 | // get the recipient-specific contentItems of the newsletter |
||||||
| 413 | 2 | $recipientContentItems = $this->getRecipientSpecificNewsletterContentItems($recipient); |
|||||
| 414 | |||||||
| 415 | // merge the recipient-specific and the general content items. recipient-specific first/at the top! |
||||||
| 416 | 2 | $recipientParams[self::CONTENT_ITEMS] = $this->orderContentItems(array_merge($recipientContentItems, $params[self::CONTENT_ITEMS])); |
|||||
| 417 | 2 | $recipientParams['_locale'] = $recipient->getPreferredLocale(); |
|||||
| 418 | |||||||
| 419 | 2 | if (0 == sizeof($recipientParams[self::CONTENT_ITEMS])) { |
|||||
| 420 | 1 | return $recipient->getEmail(); |
|||||
| 421 | } |
||||||
| 422 | |||||||
| 423 | 1 | $subject = $this->getRecipientSpecificNewsletterSubject($params[self::CONTENT_ITEMS], $recipientContentItems, $params, $recipient, $recipient->getPreferredLocale()); |
|||||
| 424 | |||||||
| 425 | // render and send the email with the right wrapper-template |
||||||
| 426 | 1 | $sent = $this->mailer->sendSingleEmail($recipient->getEmail(), $recipient->getDisplayName(), $subject, $recipientParams, $wrapperTemplate.'.txt.twig', $recipient->getPreferredLocale()); |
|||||
| 427 | |||||||
| 428 | 1 | if ($sent) { |
|||||
| 429 | // save that this recipient has recieved the newsletter |
||||||
| 430 | 1 | return null; |
|||||
| 431 | } |
||||||
| 432 | |||||||
| 433 | 1 | return $recipient->getEmail(); |
|||||
| 434 | } |
||||||
| 435 | |||||||
| 436 | /** |
||||||
| 437 | * Get the Notifications that have not yet been sent yet. |
||||||
| 438 | * Ordered by "template" and "title". |
||||||
| 439 | * |
||||||
| 440 | * @param RecipientInterface $recipient |
||||||
| 441 | * |
||||||
| 442 | * @return array of Notification |
||||||
| 443 | */ |
||||||
| 444 | 3 | protected function getNotificationsFor(RecipientInterface $recipient) |
|||||
| 445 | { |
||||||
| 446 | // get the notification mode |
||||||
| 447 | 3 | $notificationMode = $recipient->getNotificationMode(); |
|||||
| 448 | |||||||
| 449 | // get the date/time of the last notification |
||||||
| 450 | 3 | $lastNotificationDate = $this->getNotificationRepository()->getLastNotificationDate($recipient->getId()); |
|||||
| 451 | |||||||
| 452 | 3 | $sendNotifications = false; |
|||||
| 453 | 3 | $timeDelta = time() - $lastNotificationDate->getTimestamp(); |
|||||
| 454 | |||||||
| 455 | 3 | if (RecipientInterface::NOTIFICATION_MODE_IMMEDIATELY == $notificationMode) { |
|||||
| 456 | 3 | $sendNotifications = true; |
|||||
| 457 | 3 | } elseif (RecipientInterface::NOTIFICATION_MODE_HOURLY == $notificationMode) { |
|||||
| 458 | 3 | $sendNotifications = ($timeDelta > $this->getHourInterval()); |
|||||
| 459 | 3 | } elseif (RecipientInterface::NOTIFICATION_MODE_DAYLY == $notificationMode) { |
|||||
| 460 | 3 | $sendNotifications = ($timeDelta > $this->getDayInterval()); |
|||||
| 461 | } elseif (RecipientInterface::NOTIFICATION_MODE_NEVER == $notificationMode) { |
||||||
| 462 | $this->markAllNotificationsAsSentFarInThePast($recipient); |
||||||
| 463 | |||||||
| 464 | return array(); |
||||||
| 465 | } |
||||||
| 466 | |||||||
| 467 | // regularly sent notifications now |
||||||
| 468 | 3 | if ($sendNotifications) { |
|||||
| 469 | 3 | $notifications = $this->getNotificationRepository()->getNotificationsToSend($recipient->getId()); |
|||||
| 470 | |||||||
| 471 | // if notifications exist, that should be sent immediately, then send those now disregarding the users mailing-preferences. |
||||||
| 472 | } else { |
||||||
| 473 | $notifications = $this->getNotificationRepository()->getNotificationsToSendImmediately($recipient->getId()); |
||||||
| 474 | } |
||||||
| 475 | |||||||
| 476 | 3 | return $notifications; |
|||||
| 477 | } |
||||||
| 478 | |||||||
| 479 | /** |
||||||
| 480 | * Get all IDs for Recipients of pending notifications. |
||||||
| 481 | * |
||||||
| 482 | * @return array of IDs |
||||||
| 483 | */ |
||||||
| 484 | 3 | protected function getNotificationRecipientIds() |
|||||
| 485 | { |
||||||
| 486 | 3 | return $this->getNotificationRepository()->getNotificationRecipientIds(); |
|||||
| 487 | } |
||||||
| 488 | |||||||
| 489 | /** |
||||||
| 490 | * Update (set sent = now) and save the notifications. |
||||||
| 491 | * |
||||||
| 492 | * @param array $notifications |
||||||
| 493 | */ |
||||||
| 494 | 2 | protected function setNotificationsAsSent(array $notifications) |
|||||
| 495 | { |
||||||
| 496 | 2 | foreach ($notifications as $notification) { |
|||||
| 497 | 2 | $notification->setSent(new \DateTime()); |
|||||
| 498 | 2 | $this->managerRegistry->getManager()->persist($notification); |
|||||
| 499 | } |
||||||
| 500 | 2 | $this->managerRegistry->getManager()->flush(); |
|||||
| 501 | 2 | } |
|||||
| 502 | |||||||
| 503 | /** |
||||||
| 504 | * Mark all Notifications as sent long ago, as the recipient never want's to get any notifications. |
||||||
| 505 | * |
||||||
| 506 | * @param RecipientInterface $recipient |
||||||
| 507 | */ |
||||||
| 508 | 1 | protected function markAllNotificationsAsSentFarInThePast(RecipientInterface $recipient) |
|||||
| 509 | { |
||||||
| 510 | 1 | $this->getNotificationRepository()->markAllNotificationsAsSentFarInThePast($recipient->getId()); |
|||||
| 511 | 1 | } |
|||||
| 512 | |||||||
| 513 | /** |
||||||
| 514 | * Get the interval in days between newsletter mailings. |
||||||
| 515 | */ |
||||||
| 516 | 1 | protected function getNewsletterInterval() |
|||||
| 517 | { |
||||||
| 518 | 1 | return $this->configParameter[AzineEmailExtension::NEWSLETTER.'_'.AzineEmailExtension::NEWSLETTER_INTERVAL]; |
|||||
| 519 | } |
||||||
| 520 | |||||||
| 521 | /** |
||||||
| 522 | * Get the time of the day when the newsletter should be sent. |
||||||
| 523 | * |
||||||
| 524 | * @return string Time of the day in the format HH:mm |
||||||
| 525 | */ |
||||||
| 526 | 1 | protected function getNewsletterSendTime() |
|||||
| 527 | { |
||||||
| 528 | 1 | return $this->configParameter[AzineEmailExtension::NEWSLETTER.'_'.AzineEmailExtension::NEWSLETTER_SEND_TIME]; |
|||||
| 529 | } |
||||||
| 530 | |||||||
| 531 | /** |
||||||
| 532 | * Get the DateTime at which the last newsletter mailing probably has taken place, if a newsletter is sent today. |
||||||
| 533 | * (Calculated: send-time-today - interval in days). |
||||||
| 534 | * |
||||||
| 535 | * @return \DateTime |
||||||
| 536 | */ |
||||||
| 537 | 1 | protected function getDateTimeOfLastNewsletter() |
|||||
| 538 | { |
||||||
| 539 | 1 | return new \DateTime($this->getNewsletterInterval().' days ago '.$this->getNewsletterSendTime()); |
|||||
| 540 | } |
||||||
| 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() |
|||||
| 547 | { |
||||||
| 548 | 1 | return new \DateTime('+'.$this->getNewsletterInterval().' days '.$this->getNewsletterSendTime()); |
|||||
| 549 | } |
||||||
| 550 | |||||||
| 551 | /** |
||||||
| 552 | * Convenience-function to add and save a Notification-entity. |
||||||
| 553 | * |
||||||
| 554 | * @param int $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 int $importance important messages are at the top of the notification-emails, un-important at the bottom |
||||||
| 560 | * @param bool $sendImmediately whether or not to ignore the recipients mailing-preference and send the notification a.s.a.p. |
||||||
| 561 | * |
||||||
| 562 | * @return Notification |
||||||
| 563 | */ |
||||||
| 564 | 2 | public function addNotification($recipientId, $title, $content, $template, $templateVars, $importance, $sendImmediately) |
|||||
| 565 | { |
||||||
| 566 | 2 | $notification = new Notification(); |
|||||
| 567 | 2 | $notification->setRecipientId($recipientId); |
|||||
| 568 | 2 | $notification->setTitle($title); |
|||||
| 569 | 2 | $notification->setContent($content); |
|||||
| 570 | 2 | $notification->setTemplate($template); |
|||||
| 571 | 2 | $notification->setImportance($importance); |
|||||
| 572 | 2 | $notification->setSendImmediately($sendImmediately); |
|||||
| 573 | 2 | $notification->setVariables($templateVars); |
|||||
| 574 | 2 | $this->managerRegistry->getManager()->persist($notification); |
|||||
| 575 | 2 | $this->managerRegistry->getManager()->flush($notification); |
|||||
| 576 | |||||||
| 577 | 2 | return $notification; |
|||||
| 578 | } |
||||||
| 579 | |||||||
| 580 | /** |
||||||
| 581 | * Convenience-function to add and save a Notification-entity for a message => see AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TYPE. |
||||||
| 582 | * |
||||||
| 583 | * The following default are used: |
||||||
| 584 | * $importance = NORMAL |
||||||
| 585 | * $sendImmediately = fale |
||||||
| 586 | * $template = template for type AzineTemplateProvider::CONTENT_ITEM_MESSAGE_TYPE |
||||||
| 587 | * $templateVars = only those from the template-provider |
||||||
| 588 | * |
||||||
| 589 | * @param int $recipientId |
||||||
| 590 | * @param string $title |
||||||
| 591 | * @param string $content nl2br will be applied in the html-version of the email |
||||||
| 592 | * @param string $goToUrl if this is supplied, a link "Go to message" will be added |
||||||
| 593 | */ |
||||||
| 594 | 1 | public function addNotificationMessage($recipientId, $title, $content, $goToUrl = null) |
|||||
| 595 | { |
||||||
| 596 | 1 | $contentItemTemplate = $this->configParameter[AzineEmailExtension::TEMPLATES.'_'.AzineEmailExtension::CONTENT_ITEM_TEMPLATE]; |
|||||
| 597 | 1 | $templateVars = array(); |
|||||
| 598 | 1 | if (null !== $goToUrl && strlen($goToUrl) > 0) { |
|||||
| 599 | 1 | $templateVars['goToUrl'] = $goToUrl; |
|||||
| 600 | } |
||||||
| 601 | 1 | $this->addNotification($recipientId, $title, $content, $contentItemTemplate, $this->templateProvider->addTemplateVariablesFor($contentItemTemplate, $templateVars), Notification::IMPORTANCE_NORMAL, false); |
|||||
| 602 | 1 | } |
|||||
| 603 | |||||||
| 604 | /** |
||||||
| 605 | * @return NotificationRepository |
||||||
| 606 | */ |
||||||
| 607 | 4 | protected function getNotificationRepository() |
|||||
| 608 | { |
||||||
| 609 | 4 | return $this->managerRegistry->getRepository('AzineEmailBundle:Notification'); |
|||||
| 610 | } |
||||||
| 611 | } |
||||||
| 612 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.