src/Component/Core/Api/EventSubscriber/VendorSlugEventSubscriber.php line 37

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\Messenger\Command\VendorSlugAwareInterface;
  12. use BitBag\OpenMarketplace\Component\Vendor\Entity\VendorInterface;
  13. use BitBag\OpenMarketplace\Component\Vendor\Generator\SlugGeneratorInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\ViewEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. final class VendorSlugEventSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private SlugGeneratorInterface $vendorSlugGenerator
  22.     ) {
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             KernelEvents::VIEW => ['generateSlug'EventPriorities::PRE_VALIDATE],
  28.         ];
  29.     }
  30.     public function generateSlug(ViewEvent $event): void
  31.     {
  32.         $vendorSlugAware $event->getControllerResult();
  33.         if (!$vendorSlugAware instanceof VendorInterface &&
  34.             !$vendorSlugAware instanceof VendorSlugAwareInterface
  35.         ) {
  36.             return;
  37.         }
  38.         if (empty($vendorSlugAware->getCompanyName())) {
  39.             return;
  40.         }
  41.         $method $event->getRequest()->getMethod();
  42.         if (!in_array($method, [Request::METHOD_POSTRequest::METHOD_PUT], true)) {
  43.             return;
  44.         }
  45.         $slug $this->vendorSlugGenerator->generateSlug($vendorSlugAware->getCompanyName());
  46.         $vendorSlugAware->setSlug($slug);
  47.     }
  48. }