vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/CheckoutManager/CheckoutManagerFactory.php line 187

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\V7\CheckoutManager;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\V7\CheckoutManagerInterface;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\V7\HandlePendingPayments\CancelPaymentOrRecreateOrderStrategy;
  20. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  21. use Pimcore\Bundle\EcommerceFrameworkBundle\OrderManager\OrderManagerLocatorInterface;
  22. use Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\V7\Payment\PaymentInterface;
  23. use Symfony\Component\DependencyInjection\ServiceLocator;
  24. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  25. use Symfony\Component\OptionsResolver\OptionsResolver;
  26. class CheckoutManagerFactory implements CheckoutManagerFactoryInterface
  27. {
  28.     /**
  29.      * @var EnvironmentInterface
  30.      */
  31.     protected $environment;
  32.     /**
  33.      * @var OrderManagerLocatorInterface
  34.      */
  35.     protected $orderManagers;
  36.     /**
  37.      * @var CommitOrderProcessorLocatorInterface
  38.      */
  39.     protected $commitOrderProcessors;
  40.     /**
  41.      * Array of checkout step definitions
  42.      *
  43.      * @var array
  44.      */
  45.     protected $checkoutStepDefinitions = [];
  46.     /**
  47.      * @var PaymentInterface
  48.      */
  49.     protected $paymentProvider;
  50.     /**
  51.      * @var CheckoutManagerInterface[]
  52.      */
  53.     protected $checkoutManagers = [];
  54.     /**
  55.      * @var ServiceLocator|null
  56.      */
  57.     protected $handlePendingPaymentStrategyLocator;
  58.     /**
  59.      * @var string
  60.      */
  61.     protected $className CheckoutManager::class;
  62.     protected $handlePendingPaymentStrategy null;
  63.     /**
  64.      * @var EventDispatcherInterface
  65.      */
  66.     protected $eventDispatcher null;
  67.     public function __construct(
  68.         EnvironmentInterface $environment,
  69.         OrderManagerLocatorInterface $orderManagers,
  70.         CommitOrderProcessorLocatorInterface $commitOrderProcessors,
  71.         array $checkoutStepDefinitions,
  72.         PaymentInterface $paymentProvider null,
  73.         array $options = [],
  74.         ServiceLocator $handlePendingPaymentStrategyLocator null,
  75.         EventDispatcherInterface $eventDispatcher null
  76.     ) {
  77.         $this->environment $environment;
  78.         $this->orderManagers $orderManagers;
  79.         $this->commitOrderProcessors $commitOrderProcessors;
  80.         $this->paymentProvider $paymentProvider;
  81.         $this->handlePendingPaymentStrategyLocator $handlePendingPaymentStrategyLocator;
  82.         $this->eventDispatcher $eventDispatcher;
  83.         $this->processOptions($options);
  84.         $this->processCheckoutStepDefinitions($checkoutStepDefinitions);
  85.     }
  86.     protected function processOptions(array $options)
  87.     {
  88.         $resolver = new OptionsResolver();
  89.         $this->configureOptions($resolver);
  90.         $options $resolver->resolve($options);
  91.         if (isset($options['class'])) {
  92.             $this->className $options['class'];
  93.         }
  94.         if (isset($options['handle_pending_payments_strategy']) && $this->handlePendingPaymentStrategyLocator) {
  95.             $this->handlePendingPaymentStrategy $this->handlePendingPaymentStrategyLocator->get($options['handle_pending_payments_strategy']);
  96.         }
  97.     }
  98.     protected function processCheckoutStepDefinitions(array $checkoutStepDefinitions)
  99.     {
  100.         $stepResolver = new OptionsResolver();
  101.         $this->configureStepOptions($stepResolver);
  102.         foreach ($checkoutStepDefinitions as $checkoutStepDefinition) {
  103.             $this->checkoutStepDefinitions[] = $stepResolver->resolve($checkoutStepDefinition);
  104.         }
  105.     }
  106.     protected function configureOptions(OptionsResolver $resolver)
  107.     {
  108.         $this->configureClassOptions($resolver);
  109.     }
  110.     protected function configureStepOptions(OptionsResolver $resolver)
  111.     {
  112.         $resolver->setDefined('class');
  113.         $resolver->setAllowedTypes('class''string');
  114.         $resolver->setRequired('class');
  115.         $resolver->setDefined('options');
  116.         $resolver->setAllowedTypes('options', ['array''null']);
  117.     }
  118.     protected function configureClassOptions(OptionsResolver $resolver)
  119.     {
  120.         $resolver->setDefined('class');
  121.         $resolver->setAllowedTypes('class''string');
  122.         $resolver->setDefined('handle_pending_payments_strategy');
  123.         $resolver->setAllowedTypes('handle_pending_payments_strategy''string');
  124.         $resolver->setDefault('handle_pending_payments_strategy'CancelPaymentOrRecreateOrderStrategy::class);
  125.         $resolver->setRequired(['handle_pending_payments_strategy']);
  126.     }
  127.     public function createCheckoutManager(CartInterface $cart): CheckoutManagerInterface
  128.     {
  129.         $cartId $cart->getId();
  130.         if (isset($this->checkoutManagers[$cartId])) {
  131.             return $this->checkoutManagers[$cartId];
  132.         }
  133.         $checkoutSteps = [];
  134.         foreach ($this->checkoutStepDefinitions as $checkoutStepDefinition) {
  135.             $checkoutSteps[] = $this->buildCheckoutStep($cart$checkoutStepDefinition);
  136.         }
  137.         $className $this->className;
  138.         $checkoutManager = new $className(
  139.             $cart,
  140.             $this->environment,
  141.             $this->orderManagers,
  142.             $this->commitOrderProcessors,
  143.             $checkoutSteps,
  144.             $this->eventDispatcher,
  145.             $this->paymentProvider
  146.         );
  147.         $checkoutManager->setHandlePendingPaymentsStrategy($this->handlePendingPaymentStrategy);
  148.         $this->checkoutManagers[$cartId] = $checkoutManager;
  149.         return $this->checkoutManagers[$cartId];
  150.     }
  151.     protected function buildCheckoutStep(CartInterface $cart, array $checkoutStepDefinition): CheckoutStepInterface
  152.     {
  153.         $className $checkoutStepDefinition['class'];
  154.         if (!class_exists($className)) {
  155.             throw new \InvalidArgumentException(sprintf(
  156.                 'Checkout step class "%s" does not exist',
  157.                 $className
  158.             ));
  159.         }
  160.         $step = new $className($cart$checkoutStepDefinition['options'] ?? []);
  161.         return $step;
  162.     }
  163. }