src/Component/Core/Api/EventSubscriber/VendorAwareEventSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file has been created by developers from BitBag.
  4.  * Feel free to contact us once you face any issues or want to start
  5.  * You can find more information about us on https://bitbag.io and write us
  6.  * an email on hello@bitbag.io.
  7.  */
  8. declare(strict_types=1);
  9. namespace BitBag\OpenMarketplace\Component\Core\Api\EventSubscriber;
  10. use ApiPlatform\Core\EventListener\EventPriorities;
  11. use BitBag\OpenMarketplace\Component\Core\Api\Context\VendorContextInterface;
  12. use BitBag\OpenMarketplace\Component\Vendor\VendorAwareInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. final class VendorAwareEventSubscriber implements EventSubscriberInterface
  18. {
  19.     public function __construct(
  20.         private VendorContextInterface $vendorContext
  21.     ) {
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             KernelEvents::VIEW => ['setVendorFromCurrentContext'EventPriorities::PRE_VALIDATE],
  27.         ];
  28.     }
  29.     public function setVendorFromCurrentContext(ViewEvent $event): void
  30.     {
  31.         $vendorAware $event->getControllerResult();
  32.         if (!$vendorAware instanceof VendorAwareInterface) {
  33.             return;
  34.         }
  35.         $method $event->getRequest()->getMethod();
  36.         if (!in_array($method, [Request::METHOD_POST], true)) {
  37.             return;
  38.         }
  39.         $vendor $this->vendorContext->getVendor();
  40.         if (null === $vendor) {
  41.             return;
  42.         }
  43.         $vendorAware->setVendor($vendor);
  44.     }
  45. }