vendor/sylius/sylius/src/Sylius/Bundle/ApiBundle/EventSubscriber/ProductVariantEventSubscriber.php line 39

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\Component\Core\Event\ProductVariantCreated;
  14. use Sylius\Component\Core\Event\ProductVariantUpdated;
  15. use Sylius\Component\Core\Model\ProductVariantInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Event\ViewEvent;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\Messenger\MessageBusInterface;
  21. final class ProductVariantEventSubscriber implements EventSubscriberInterface
  22. {
  23.     public function __construct(private MessageBusInterface $eventBus)
  24.     {
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             KernelEvents::VIEW => ['postWrite'EventPriorities::POST_WRITE],
  30.         ];
  31.     }
  32.     public function postWrite(ViewEvent $event): void
  33.     {
  34.         $variant $event->getControllerResult();
  35.         $method $event->getRequest()->getMethod();
  36.         if (!$variant instanceof ProductVariantInterface) {
  37.             return;
  38.         }
  39.         if ($method === Request::METHOD_POST) {
  40.             $this->eventBus->dispatch(new ProductVariantCreated($variant->getCode()));
  41.             return;
  42.         }
  43.         if ($method === Request::METHOD_PUT) {
  44.             $this->eventBus->dispatch(new ProductVariantUpdated($variant->getCode()));
  45.         }
  46.     }
  47. }