src/FMT/Domain/EventSubscriber/Cart/CartActionSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. namespace FMT\Domain\EventSubscriber\Cart;
  3. use FMT\Domain\Event\CartActionEvent;
  4. use FMT\Domain\Exception\CartConfigurationException;
  5. use FMT\Domain\Service\CartProcessorInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. /**
  8.  * Class CartActionSubscriber
  9.  * @package FMT\Domain\EventSubscriber\Cart
  10.  */
  11. class CartActionSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var CartProcessorInterface[]
  15.      */
  16.     private $processors = [];
  17.     public static function getSubscribedEvents()
  18.     {
  19.         // return the subscribed events, their methods and priorities
  20.         return [
  21.             CartActionEvent::ADD_PRODUCT => [
  22.                 ['recalculateOrder']
  23.             ],
  24.             CartActionEvent::REMOVE_PRODUCT => [
  25.                 ['recalculateOrder']
  26.             ],
  27.             CartActionEvent::ESTIMATE_CART => [
  28.                 ['recalculateOrder']
  29.             ],
  30.         ];
  31.     }
  32.     /**
  33.      * @param CartActionEvent $event
  34.      */
  35.     public function recalculateOrder(CartActionEvent $event)
  36.     {
  37.         foreach ($this->processors as $processor) {
  38.             $processor->process($event->getCart());
  39.         }
  40.     }
  41.     #region Internal
  42.     /**
  43.      * @internal
  44.      *
  45.      * @param CartProcessorInterface $processor
  46.      * @param string $alias
  47.      * @throws CartConfigurationException
  48.      */
  49.     public function addProcessor(CartProcessorInterface $processorstring $alias)
  50.     {
  51.         if (array_key_exists($alias$this->processors)) {
  52.             throw new CartConfigurationException(sprintf('Multiple cart processors with alias "%s"'$alias));
  53.         }
  54.         $this->processors[] = $processor;
  55.     }
  56.     #endregion
  57. }