Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Créneaux : Rappel : refactoring de l'envoi via l'EmailingEventListener #1044

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ services:
- { name: kernel.event_listener, event: shift.reserved, method: onShiftReserved }
- { name: kernel.event_listener, event: shift.booked, method: onShiftBooked }
- { 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: member.cycle.start, method: onMemberCycleStart }
- { name: kernel.event_listener, event: member.cycle.half, method: onMemberCycleHalf }
Expand Down
2 changes: 0 additions & 2 deletions src/AppBundle/Command/ShiftGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
$mailer = $this->getContainer()->get('mailer');
$router = $this->getContainer()->get('router');

$admin = $em->getRepository('AppBundle:User')->findSuperAdminAccount();
$use_fly_and_fixed = $this->getContainer()->getParameter('use_fly_and_fixed');
Expand Down
39 changes: 11 additions & 28 deletions src/AppBundle/Command/ShiftReminderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AppBundle\Command;

use AppBundle\Entity\Shift;
use AppBundle\Event\ShiftReminderEvent;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -22,19 +23,15 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();

$from_given = $input->getArgument('date');
$from = date_create_from_format('Y-m-d',$from_given);
if (!$from || $from->format('Y-m-d') != $from_given) {
$output->writeln('<fg=red;> wrong date format. Use Y-m-d </>');
return;
}

$count = 0;

$output->writeln('<fg=cyan;>'.$from->format('d M Y').'</>');
////////////////////////
$em = $this->getContainer()->get('doctrine')->getManager();
$mailer = $this->getContainer()->get('mailer');

$qb = $em->getRepository('AppBundle:Shift')->createQueryBuilder('s')
->where('s.start >= :start')
Expand All @@ -43,34 +40,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
->setParameter('end', $from->add(\DateInterval::createFromDateString('+1 day'))->format('Y-m-d'));

$shifts = $qb->getQuery()->getResult();
$shiftEmail = $this->getContainer()->getParameter('emails.shift');

$dynamicContent = $em->getRepository('AppBundle:DynamicContent')->findOneByCode("SHIFT_REMINDER_EMAIL")->getContent();
$template = $this->getContainer()->get('twig')->createTemplate($dynamicContent);
$message = 'Shift reminder for ' . count($shifts) . ' créneau' . ((count($shifts)>1) ? 'x':'');
$output->writeln('<fg=cyan;>'.$message.'</>');

/** @var Shift $shift */
$count_reminder_sent = 0;
$dispatcher = $this->getContainer()->get('event_dispatcher');
foreach ($shifts as $shift) {
if ($shift->getShifter()) { //send reminder
$dynamicContent = $this->getContainer()->get('twig')->render($template, array('beneficiary' => $shift->getShifter()));
$reminder = (new \Swift_Message('[ESPACE MEMBRES] Ton créneau'))
->setFrom($shiftEmail['address'], $shiftEmail['from_name'])
->setTo($shift->getShifter()->getEmail())
->setBody(
$this->getContainer()->get('twig')->render(
'emails/shift_reminder.html.twig',
array(
'shift' => $shift,
'dynamicContent' => $dynamicContent
)
),
'text/html'
);
$mailer->send($reminder);
$count++;
if ($shift->getShifter()) {
$dispatcher->dispatch(ShiftReminderEvent::NAME, new ShiftReminderEvent($shift));
$count_reminder_sent++;
}
}

$message = $count.' email'.(($count>1) ? 's':'').' envoyé'.(($count>1) ? 's':'');
$message = $count_reminder_sent . ' email' . (($count_reminder_sent>1) ? 's':'') . ' envoyé' . (($count_reminder_sent>1) ? 's':'');
$output->writeln('<fg=cyan;>>>></><fg=green;> '.$message.' </>');
}
}
23 changes: 23 additions & 0 deletions src/AppBundle/Event/ShiftReminderEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace AppBundle\Event;

use AppBundle\Entity\Shift;
use Symfony\Component\EventDispatcher\Event;

class ShiftReminderEvent extends Event
{
const NAME = 'shift.reminder';

private $shift;

public function __construct(Shift $shift)
{
$this->shift = $shift;
}

public function getShift()
{
return $this->shift;
}
}
39 changes: 39 additions & 0 deletions src/AppBundle/EventListener/EmailingEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use AppBundle\Event\ShiftReservedEvent;
use AppBundle\Event\ShiftBookedEvent;
use AppBundle\Event\ShiftFreedEvent;
use AppBundle\Event\ShiftReminderEvent;
use AppBundle\Event\ShiftDeletedEvent;
use Doctrine\ORM\EntityManagerInterface;
use Monolog\Logger;
Expand Down Expand Up @@ -373,6 +374,44 @@ public function onShiftFreed(ShiftFreedEvent $event)
}
}

/**
* @param ShiftReminderEvent $event
* @throws \Exception
*/
public function onShiftReminder(ShiftReminderEvent $event)
{
$this->logger->info("Emailing Listener: onShiftReminder");

$shift = $event->getShift();
$beneficiary = $shift->getShifter();

$dynamicContent = $this->em->getRepository('AppBundle:DynamicContent')->findOneByCode("SHIFT_REMINDER_EMAIL")->getContent();
$template = $this->container->get('twig')->createTemplate($dynamicContent);
$dynamicContent = $this->container->get('twig')->render($template, array('beneficiary' => $beneficiary));

// send a reminder e-mail to the beneficiary
if ($beneficiary) {
$emailObject = '[ESPACE MEMBRES] Ton créneau';
$emailTo = $beneficiary->getEmail();

$email = (new \Swift_Message($emailObject))
->setFrom($this->shift_email['address'], $this->shift_email['from_name'])
->setTo($emailTo)
->setBody(
$this->renderView(
'emails/shift_reminder.html.twig',
array(
'shift' => $shift,
'dynamicContent' => $dynamicContent
)
),
'text/html'
);

$this->mailer->send($email);
}
}

/**
* @param ShiftDeletedEvent $event
* @throws \Exception
Expand Down