diff --git a/app/config/services.yml b/app/config/services.yml index dd7a22cd1..1c748592f 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -167,6 +167,7 @@ services: - { name: kernel.event_listener, event: shift.freed, method: onShiftFreed } - { name: kernel.event_listener, event: shift.reminder, method: onShiftReminder } - { name: kernel.event_listener, event: shift.deleted, method: onShiftDeleted } + - { name: kernel.event_listener, event: shift.alerts, method: onShiftAlerts } - { name: kernel.event_listener, event: member.cycle.start, method: onMemberCycleStart } - { name: kernel.event_listener, event: member.cycle.half, method: onMemberCycleHalf } - { name: kernel.event_listener, event: member.created, method: onMemberCreated } @@ -177,6 +178,14 @@ services: - { name: kernel.event_listener, event: helloasso.registration_success, method: onHelloassoRegistrationSuccess } - { name: kernel.event_listener, event: helloasso.too_early, method: onHelloassoTooEarly } - { name: kernel.event_listener, event: code.new, method: onCodeNew } + mattermost_event_listener: + class: AppBundle\EventListener\MattermostEventListener + arguments: + $entityManager: "@doctrine.orm.entity_manager" + $logger: "@logger" + $container: "@service_container" + tags: + - { name: kernel.event_listener, event: shift.alerts.mattermost, method: onShiftAlerts } # subscribers beneficiary_initialization_subscriber: diff --git a/src/AppBundle/Command/SendShiftAlertsCommand.php b/src/AppBundle/Command/SendShiftAlertsCommand.php index 9ce262a47..6919c9ea0 100644 --- a/src/AppBundle/Command/SendShiftAlertsCommand.php +++ b/src/AppBundle/Command/SendShiftAlertsCommand.php @@ -4,14 +4,14 @@ use AppBundle\Entity\ShiftAlert; use AppBundle\Entity\ShiftBucket; +use AppBundle\Event\ShiftAlertsEvent; +use AppBundle\Event\ShiftAlertsMattermostEvent; use DateTime; -use Swift_Message; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\HttpClient\HttpClient; class SendShiftAlertsCommand extends ContainerAwareCommand { @@ -33,24 +33,38 @@ protected function execute(InputInterface $input, OutputInterface $output) { $date_given = $input->getArgument('date'); $jobs = explode(',', $input->getArgument('jobs')); + $emails = $input->getOption('emails') ? explode(',', $input->getOption('emails')) : null; $email_template = $input->getOption('emailTemplate'); + $mattermost_hook_url = $input->getOption('mattermostUrl'); $mattermost_template = $input->getOption('mattermostTemplate'); $date = date_create_from_format('Y-m-d', $date_given); if (!$date || $date->format('Y-m-d') != $date_given) { - $output->writeln(' wrong date format. Use Y-m-d '); + $output->writeln('Wrong date format. Use Y-m-d '); return; } $date->setTime(0, 0); $alerts = $this->computeAlerts($date, $jobs); - $nbAlerts = count($alerts); - if ($nbAlerts > 0) { - $output->writeln('Found ' . $nbAlerts . ' alerts to send'); - $this->sendAlertsToMattermost($input, $output, $date, $alerts, $mattermost_template); - $this->sendAlertsByEmail($input, $output, $date, $alerts, $email_template); + if (count($alerts) > 0) { + $output->writeln('Found ' . count($alerts) . ' alert' . ((count($alerts)>1)?'s':'') . ' to send'); + + // email + if ($emails) { + $dispatcher = $this->getContainer()->get('event_dispatcher'); + $dispatcher->dispatch(ShiftAlertsEvent::NAME, new ShiftAlertsEvent($alerts, $date, $email_template, $emails)); + $output->writeln('Email(s) sent'); + } + + // mattermost + if ($mattermost_hook_url) { + $dispatcher = $this->getContainer()->get('event_dispatcher'); + $dispatcher->dispatch(ShiftAlertsMattermostEvent::NAME, new ShiftAlertsMattermostEvent($alerts, $date, $mattermost_template, $mattermost_hook_url)); + $output->writeln('Alerts posted on Mattermost'); + } + } else { - $output->writeln('No shift alert to send'); + $output->writeln('No shift alert to send'); } } @@ -84,62 +98,4 @@ private function computeAlerts(DateTime $date, $jobs) { } return $alerts; } - - private function sendAlertsByEmail(InputInterface $input, OutputInterface $output, DateTime $date, $alerts, $template) { - $mailer = $this->getContainer()->get('mailer'); - $recipients = $input->getOption('emails') ? explode(',', $input->getOption('emails')) : null; - if ($recipients) { - $dateFormatted = strftime("%A %e %B", $date->getTimestamp()); - $subject = '[ALERTE CRENEAUX] ' . $dateFormatted; - - $shiftEmail = $this->getContainer()->getParameter('emails.shift'); - - $em = $this->getContainer()->get('doctrine')->getManager(); - $dynamicContent = $em->getRepository('AppBundle:DynamicContent')->findOneByCode($template); - $template = null; - if ($dynamicContent) { - $template = $this->getContainer()->get('twig')->createTemplate($dynamicContent->getContent()); - } else { - $template = 'emails/shift_alerts_default.html.twig'; - } - - $email = (new Swift_Message($subject)) - ->setFrom($shiftEmail['address'], $shiftEmail['from_name']) - ->setTo($recipients) - ->setBody( - $this->getContainer()->get('twig')->render( - $template, - array('alerts' => $alerts, 'date' => $date) - ), - 'text/html' - ); - $mailer->send($email); - $output->writeln('Email(s) sent'); - } - } - - private function sendAlertsToMattermost(InputInterface $input, OutputInterface $output, DateTime $date, $alerts, $template) { - $mmHookUrl = $input->getOption('mattermostUrl'); - if ($mmHookUrl != null) { - $em = $this->getContainer()->get('doctrine')->getManager(); - $dynamicContent = $em->getRepository('AppBundle:DynamicContent')->findOneByCode($template); - $template = null; - if ($dynamicContent) { - $template = $this->getContainer()->get('twig')->createTemplate($dynamicContent->getContent()); - } else { - $template = 'markdown/shift_alerts_default.md.twig'; - } - $content = $this->getContainer()->get('twig')->render( - $template, - array('alerts' => $alerts, 'date' => $date) - ); - - $client = HttpClient::create(); - $response = $client->request('POST', $mmHookUrl, [ - 'json' => ['text' => $content] - ]); - } - $output->writeln('Alerts posted on Mattermost'); - } - } diff --git a/src/AppBundle/Event/ShiftAlertsEvent.php b/src/AppBundle/Event/ShiftAlertsEvent.php new file mode 100644 index 000000000..22bc016c8 --- /dev/null +++ b/src/AppBundle/Event/ShiftAlertsEvent.php @@ -0,0 +1,55 @@ +alerts = $alerts; + $this->date = $date; + $this->template = $template; + $this->recipients = $recipients; + } + + /** + * @return array + */ + public function getAlerts() + { + return $this->alerts; + } + + /** + * @return \DateTime + */ + public function getDate() + { + return $this->date; + } + + /** + * @return string|null + */ + public function getTemplate() + { + return $this->template; + } + + /** + * @return array|null + */ + public function getRecipients() + { + return $this->recipients; + } +} diff --git a/src/AppBundle/Event/ShiftAlertsMattermostEvent.php b/src/AppBundle/Event/ShiftAlertsMattermostEvent.php new file mode 100644 index 000000000..b5a2f4d5c --- /dev/null +++ b/src/AppBundle/Event/ShiftAlertsMattermostEvent.php @@ -0,0 +1,55 @@ +alerts = $alerts; + $this->date = $date; + $this->template = $template; + $this->mattermost_hook_url = $mattermost_hook_url; + } + + /** + * @return array + */ + public function getAlerts() + { + return $this->alerts; + } + + /** + * @return \DateTime + */ + public function getDate() + { + return $this->date; + } + + /** + * @return string|null + */ + public function getTemplate() + { + return $this->template; + } + + /** + * @return string|null + */ + public function getMattermostHookUrl() + { + return $this->mattermost_hook_url; + } +} diff --git a/src/AppBundle/EventListener/EmailingEventListener.php b/src/AppBundle/EventListener/EmailingEventListener.php index d2b1308a6..98fb30891 100644 --- a/src/AppBundle/EventListener/EmailingEventListener.php +++ b/src/AppBundle/EventListener/EmailingEventListener.php @@ -12,6 +12,7 @@ use AppBundle\Event\MemberCycleEndEvent; use AppBundle\Event\MemberCycleHalfEvent; use AppBundle\Event\MemberCycleStartEvent; +use AppBundle\Event\ShiftAlertsEvent; use AppBundle\Event\ShiftReservedEvent; use AppBundle\Event\ShiftBookedEvent; use AppBundle\Event\ShiftFreedEvent; @@ -446,6 +447,45 @@ public function onShiftDeleted(ShiftDeletedEvent $event) } } + /** + * @param ShiftAlertsEvent $event + * @throws \Exception + */ + public function onShiftAlerts(ShiftAlertsEvent $event) + { + $this->logger->info("Emailing Listener: onShiftAlerts"); + + $alerts = $event->getAlerts(); + $date = $event->getDate(); + $recipients = $event->getRecipients(); + + if ($alerts && $recipients) { + $emailObject = '[ALERTE CRENEAUX] ' . strftime("%A %e %B", $date->getTimestamp()); + $emailTo = $recipients; + + $dynamicContent = $this->em->getRepository('AppBundle:DynamicContent')->findOneByCode($event->getTemplate()); + $template = null; + if ($dynamicContent) { + $template = $this->container->get('twig')->createTemplate($dynamicContent->getContent()); + } else { + $template = 'emails/shift_alerts_default.html.twig'; + } + + $email = (new \Swift_Message($emailObject)) + ->setFrom($this->shift_email['address'], $this->shift_email['from_name']) + ->setTo($emailTo) + ->setBody( + $this->container->get('twig')->render( + $template, + array('alerts' => $alerts, 'date' => $date) + ), + 'text/html' + ); + + $this->mailer->send($email); + } + } + /** * @param MemberCycleStartEvent $event * @throws \Exception diff --git a/src/AppBundle/EventListener/MattermostEventListener.php b/src/AppBundle/EventListener/MattermostEventListener.php new file mode 100644 index 000000000..88697819a --- /dev/null +++ b/src/AppBundle/EventListener/MattermostEventListener.php @@ -0,0 +1,54 @@ +em = $entityManager; + $this->logger = $logger; + $this->container = $container; + } + + /** + * @param ShiftAlertsMattermostEvent $event + * @throws \Exception + */ + public function onShiftAlerts(ShiftAlertsMattermostEvent $event) + { + $this->logger->info("Mattermost Listener: onShiftAlerts"); + + $alerts = $event->getAlerts(); + $date = $event->getDate(); + + if ($alerts && $event->getMattermostHookUrl()) { + $template = null; + $dynamicContent = $this->em->getRepository('AppBundle:DynamicContent')->findOneByCode($event->getTemplate()); + if ($dynamicContent) { + $template = $this->container->get('twig')->createTemplate($dynamicContent->getContent()); + } else { + $template = 'markdown/shift_alerts_default.md.twig'; + } + $content = $this->container->get('twig')->render( + $template, + array('alerts' => $alerts, 'date' => $date) + ); + + $client = HttpClient::create(); + $response = $client->request('POST', $event->getMattermostHookUrl(), [ + 'json' => ['text' => $content] + ]); + } + } +}