src/EventListener/PostValidate/UpdateContactStatusPostValidateTransformer.php line 72

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\PostValidate;
  3. use App\DTO\Contact\ContactOutput;
  4. use App\DTO\Contact\UpdateContactStatus;
  5. use App\Entity\Contact;
  6. use App\Entity\SiteStatus;
  7. use App\Repository\SiteStatusRepository;
  8. use App\Service\AfterReadStorage;
  9. use App\Service\Manager\ContactManager;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  15. class UpdateContactStatusPostValidateTransformer implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SiteStatusRepository
  19.      */
  20.     private $siteStatusRepository;
  21.     /**
  22.      * @var AfterReadStorage
  23.      */
  24.     private $afterReadStorage;
  25.     /**
  26.      * @var ContactManager
  27.      */
  28.     private $contactManager;
  29.     /**
  30.      * @var NormalizerInterface
  31.      */
  32.     private $normalizer;
  33.     public function __construct(
  34.         SiteStatusRepository $siteStatusRepository,
  35.         AfterReadStorage $afterReadStorage,
  36.         ContactManager $contactManager,
  37.         NormalizerInterface $normalizer
  38.     ) {
  39.         $this->siteStatusRepository $siteStatusRepository;
  40.         $this->afterReadStorage $afterReadStorage;
  41.         $this->contactManager $contactManager;
  42.         $this->normalizer $normalizer;
  43.     }
  44.     /**
  45.      * @return array
  46.      */
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             PostValidateListener::POST_VALIDATE_TRANSFORMATION => 'transform',
  51.         ];
  52.     }
  53.     /**
  54.      * @param UpdateContactStatus $payload
  55.      *
  56.      * @return bool
  57.      */
  58.     public function supportTransformation($payloadRequest $request)
  59.     {
  60.         return $payload instanceof UpdateContactStatus && $request->isMethod($request::METHOD_PUT);
  61.     }
  62.     /**
  63.      * @param ViewEvent $event
  64.      */
  65.     public function transform(ViewEvent $event)
  66.     {
  67.         /** @var UpdateContactStatus $payload */
  68.         $payload $event->getControllerResult();
  69.         if (!$this->supportTransformation($payload$event->getRequest())) {
  70.             return;
  71.         }
  72.         /** @var SiteStatus $siteStatus */
  73.         $siteStatus $this->siteStatusRepository->find($payload->statusId);
  74.         /** @var Contact $contact */
  75.         $contact $this->afterReadStorage->getObject();
  76.         $contact->setStatus($siteStatus);
  77.         $this->contactManager->save($contact);
  78.         $event->setResponse(new JsonResponse($this->normalizer->normalize($contactContactOutput::class)));
  79.     }
  80. }