src/Platform/MarketingBundle/Listeners/UpsellListener.php line 52

Open in your IDE?
  1. <?php
  2. namespace Platform\MarketingBundle\Listeners;
  3. use Cms\CoreBundle\Service\ContextManager;
  4. use Platform\MarketingBundle\Model\ProductControllerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\RouterInterface;
  10. /**
  11.  * Class UpsellListener
  12.  * @package Platform\MarketingBundle\EventListener
  13.  */
  14. final class UpsellListener implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var ContextManager
  18.      */
  19.     protected ContextManager $cm;
  20.     /**
  21.      * @var RouterInterface
  22.      */
  23.     protected RouterInterface $router;
  24.     /**
  25.      * @param ContextManager $cm
  26.      * @param RouterInterface $router
  27.      */
  28.     public function __construct(ContextManager $cmRouterInterface $router)
  29.     {
  30.         $this->cm $cm;
  31.         $this->router $router;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             KernelEvents::CONTROLLER => ['productCheck'],
  40.         ];
  41.     }
  42.     /**
  43.      * @param ControllerEvent $event
  44.      */
  45.     public function productCheck(ControllerEvent $event): void
  46.     {
  47.         // get the controller
  48.         $controller $event->getController();
  49.         // should be an array of object then method call
  50.         if (is_array($controller) && $controller[0] instanceof ProductControllerInterface) {
  51.             // must have a tenant
  52.             $tenant $this->cm->getGlobalContext()->getTenant();
  53.             if (empty($tenant)) {
  54.                 return;
  55.             }
  56.             // we know that we can call the interface method, which is static, on the controller
  57.             try {
  58.                 $result $controller[0]->productCheck(
  59.                     $tenant->getProducts()
  60.                 );
  61.                 if ( ! empty($result) && ! $result instanceof Response) {
  62.                     throw new \Exception();
  63.                 }
  64.             } catch (\Exception $e) {
  65.                 $result $e;
  66.             }
  67.             // if we have a non-empty result, need to reset the controller
  68.             // also need to stop propagation
  69.             if ( ! empty($result)) {
  70.                 $event->stopPropagation();
  71.                 $event->setController(
  72.                     function () use ($result) {
  73.                         if ($result instanceof \Exception) {
  74.                             throw $result;
  75.                         }
  76.                         return $result;
  77.                     }
  78.                 );
  79.             }
  80.         }
  81.     }
  82. }