app = $app; $this->messagePublisher = $this->app['alchemy_worker.message.publisher']; $this->logger = $this->app['alchemy_worker.logger']; } public function process(array $payload) { $this->setDelivererLocator(new LazyLocator($this->app, 'notification.deliverer')); $days = (int)$this->getConf()->get(['registry', 'actions', 'validation-reminder-days']); $interval = sprintf('P%dD', $days); $now = new DateTime(); $dateTo = clone($now); try { $dateTo->add(new DateInterval($interval)); } catch(\Exception $e) { $this->logger->error(sprintf('Bad interval "%s" ?', $interval)); return ; } foreach ($this->getValidationParticipantRepository()->findNotConfirmedAndNotRemindedParticipantsByExpireDate($dateTo, $now) as $participant) { $validationSession = $participant->getSession(); $basket = $validationSession->getBasket(); $canSend = true; $user = $participant->getUser(); // always ok ! try { $str_email = $user->getEmail(); // force to hydrate } catch (\Exception $e) { $this->logger->error('user not found!'); $canSend = false; } $emails[] = // find the token if exists // nb : a validation may have not generated tokens if forcing auth was required upon creation $token = null; try { $token = $this->getTokenRepository()->findValidationToken($basket, $user); } catch (\Exception $e) { // not unique token ? should not happen $canSend = false; } if(!$canSend) { continue; } if(!is_null($token)) { $url = $this->app->url('lightbox_validation', ['basket' => $basket->getId(), 'LOG' => $token->getValue()]); } else { $url = $this->app->url('lightbox_validation', ['basket' => $basket->getId()]); } $this->doRemind($participant, $basket, $url); } $this->getEntityManager()->flush(); } private function doRemind(ValidationParticipant $participant, Basket $basket, $url) { $params = [ 'from' => $basket->getValidation()->getInitiator()->getId(), 'to' => $participant->getUser()->getId(), 'ssel_id' => $basket->getId(), 'url' => $url, ]; $datas = json_encode($params); $mailed = false; $userFrom = $basket->getValidation()->getInitiator(); $userTo = $participant->getUser(); if ($this->shouldSendNotificationFor($participant->getUser(), 'eventsmanager_notify_validationreminder')) { $readyToSend = false; $title = $receiver = $emitter = null; try { $title = $basket->getName(); $receiver = Receiver::fromUser($userTo); $emitter = Emitter::fromUser($userFrom); $readyToSend = true; } catch (\Exception $e) { // no-op } if ($readyToSend) { $this->logger->info(sprintf(' -> remind "%s" from "%s" to "%s"', $title, $emitter->getEmail(), $receiver->getEmail())); $mail = MailInfoValidationReminder::create($this->app, $receiver, $emitter); $mail->setButtonUrl($params['url']); $mail->setTitle($title); $this->deliver($mail); $mailed = true; $participant->setReminded(new DateTime('now')); $this->getEntityManager()->persist($participant); } } return $this->app['events-manager']->notify($params['to'], 'eventsmanager_notify_validationreminder', $datas, $mailed); } /** * @param User $user * @param $type * @return mixed */ private function shouldSendNotificationFor(User $user, $type) { return $this->app['settings']->getUserNotificationSetting($user, $type); } /** * @return PropertyAccess */ private function getConf() { return $this->app['conf']; } /** * @return EntityManagerInterface */ private function getEntityManager() { return $this->app['orm.em']; } /** * @return ValidationParticipantRepository */ private function getValidationParticipantRepository() { return $this->app['repo.validation-participants']; } /** * @return TokenRepository */ private function getTokenRepository() { return $this->app['repo.tokens']; } }