src/EventSubscriber/JobCompletedSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\Job;
  5. use App\Entity\User;
  6. use App\Service\Message;
  7. use JetBrains\PhpStorm\ArrayShape;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use function in_array;
  14. final class JobCompletedSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var string The relative path inside templates/messages without any extensions */
  17.     private const TEMPLATE 'job/completed';
  18.     public function __construct(
  19.         private readonly TokenStorageInterface $tokenStorage,
  20.         private readonly Message $messageService,
  21.     )
  22.     {
  23.     }
  24.     #[ArrayShape([KernelEvents::VIEW => "array"])]
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             KernelEvents::VIEW => ['sendMessage'EventPriorities::POST_WRITE],
  29.         ];
  30.     }
  31.     public function sendMessage(ViewEvent $event): void
  32.     {
  33.         $job $event->getControllerResult();
  34.         $method $event->getRequest()->getMethod();
  35.         # PUT and PATCH methods are used to update instances, and we are only interested in updated jobs
  36.         if (!$job instanceof Job || !in_array($method, [Request::METHOD_PUTRequest::METHOD_PATCH])) {
  37.             return;
  38.         }
  39.         $previousData $event->getRequest()->get('previous_data');
  40.         # If the status hasn't changed, we're not interested
  41.         if ($previousData->getStatus() === $job->getStatus()) {
  42.             return;
  43.         }
  44.         # If the status hasn't changed to "unable to complete", we're not interested
  45.         if ($job->getStatus() !== Job::JOB_STATUS_COMPLETE) {
  46.             return;
  47.         }
  48.         $customer $job->getPond()->getCustomer();
  49.         $message $this->messageService
  50.             ->withRecipient($customer)
  51.             ->withSubject('We have completed your job')
  52.             ->withTemplate(self::TEMPLATE)
  53.             ->withTemplateData([
  54.                 'addressLine1' => $job->getPond()->getAddress1(),
  55.                 'firstName' => $customer->getFirstName() === 'ANNETTE' null $customer->getFirstName(),
  56.                 'fullAddress' => $job->getPond()->getAddress(),
  57.                 'reviewUrl' => 'TBC'// @todo change this when we are provided with one
  58.             ]);
  59.         $currentUser $this->tokenStorage->getToken()->getUser();
  60.         if ($currentUser instanceof User) {
  61.             $message $message->withCurrentUser($currentUser);
  62.         }
  63.         $message->send();
  64.     }
  65. }