src/Sylius/Bundle/ApiBundle/EventSubscriber/CatalogPromotionEventSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ApiBundle\EventSubscriber;
  12. use ApiPlatform\Core\EventListener\EventPriorities;
  13. use Sylius\Bundle\CoreBundle\CatalogPromotion\Announcer\CatalogPromotionAnnouncerInterface;
  14. use Sylius\Component\Core\Model\CatalogPromotionInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Event\ViewEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. final class CatalogPromotionEventSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(private CatalogPromotionAnnouncerInterface $catalogPromotionAnnouncer)
  22.     {
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             KernelEvents::VIEW => ['postWrite'EventPriorities::POST_WRITE],
  28.         ];
  29.     }
  30.     public function postWrite(ViewEvent $event): void
  31.     {
  32.         $catalogPromotion $event->getControllerResult();
  33.         if (!$catalogPromotion instanceof CatalogPromotionInterface) {
  34.             return;
  35.         }
  36.         $method $event->getRequest()->getMethod();
  37.         if ($method === Request::METHOD_POST) {
  38.             $this->catalogPromotionAnnouncer->dispatchCatalogPromotionCreatedEvent($catalogPromotion);
  39.             return;
  40.         }
  41.         if (in_array($method, [Request::METHOD_PUTRequest::METHOD_PATCH], true)) {
  42.             $this->catalogPromotionAnnouncer->dispatchCatalogPromotionUpdatedEvent($catalogPromotion);
  43.         }
  44.     }
  45. }