vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/Factory.php line 231

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\AvailabilitySystem\AvailabilitySystemInterface;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\AvailabilitySystem\AvailabilitySystemLocatorInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartInterface;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartManagerInterface;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartManagerLocatorInterface;
  20. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\CheckoutManagerFactoryLocatorInterface;
  21. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\CommitOrderProcessorInterface;
  22. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\CommitOrderProcessorLocatorInterface;
  23. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\V7\CheckoutManagerInterface;
  24. use Pimcore\Bundle\EcommerceFrameworkBundle\DependencyInjection\PimcoreEcommerceFrameworkExtension;
  25. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\FilterService;
  26. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\FilterServiceLocatorInterface;
  27. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\IndexService;
  28. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractVoucherTokenType;
  29. use Pimcore\Bundle\EcommerceFrameworkBundle\OfferTool\ServiceInterface;
  30. use Pimcore\Bundle\EcommerceFrameworkBundle\OrderManager\OrderManagerLocatorInterface;
  31. use Pimcore\Bundle\EcommerceFrameworkBundle\OrderManager\V7\OrderManagerInterface;
  32. use Pimcore\Bundle\EcommerceFrameworkBundle\PaymentManager\PaymentManagerInterface;
  33. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceSystemInterface;
  34. use Pimcore\Bundle\EcommerceFrameworkBundle\PriceSystem\PriceSystemLocatorInterface;
  35. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\PricingManagerInterface;
  36. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\PricingManagerLocatorInterface;
  37. use Pimcore\Bundle\EcommerceFrameworkBundle\Tracking\TrackingManagerInterface;
  38. use Pimcore\Bundle\EcommerceFrameworkBundle\VoucherService\TokenManager\TokenManagerInterface;
  39. use Pimcore\Bundle\EcommerceFrameworkBundle\VoucherService\VoucherServiceInterface;
  40. use Symfony\Component\DependencyInjection\ContainerInterface;
  41. class Factory
  42. {
  43.     /**
  44.      * @var ContainerInterface
  45.      */
  46.     private $container;
  47.     /**
  48.      * Tenant specific cart managers
  49.      *
  50.      * @var CartManagerLocatorInterface
  51.      */
  52.     private $cartManagers;
  53.     /**
  54.      * Tenant specific order managers
  55.      *
  56.      * @var OrderManagerLocatorInterface
  57.      */
  58.     private $orderManagers;
  59.     /**
  60.      * Pricing managers registered by tenant
  61.      *
  62.      * @var PricingManagerLocatorInterface
  63.      */
  64.     private $pricingManagers;
  65.     /**
  66.      * Price systems registered by name
  67.      *
  68.      * @var PriceSystemLocatorInterface
  69.      */
  70.     private $priceSystems;
  71.     /**
  72.      * Availability systems registered by name
  73.      *
  74.      * @var AvailabilitySystemLocatorInterface
  75.      */
  76.     private $availabilitySystems;
  77.     /**
  78.      * Checkout manager factories registered by tenant
  79.      *
  80.      * @var CheckoutManagerFactoryLocatorInterface
  81.      */
  82.     private $checkoutManagerFactories;
  83.     /**
  84.      * Commit order processors registered by tenant
  85.      *
  86.      * @var CommitOrderProcessorLocatorInterface
  87.      */
  88.     private $commitOrderProcessors;
  89.     /**
  90.      * Filter services registered by ^tenant
  91.      *
  92.      * @var FilterServiceLocatorInterface
  93.      */
  94.     private $filterServices;
  95.     /**
  96.      * Systems with multiple instances (e.g. price systems or tenant specific systems) are
  97.      * injected through a service locator which is indexed by tenant/name. All other services
  98.      * are loaded from the container on demand to make sure only services needed are built.
  99.      *
  100.      * @param ContainerInterface $container
  101.      * @param CartManagerLocatorInterface $cartManagers
  102.      * @param OrderManagerLocatorInterface $orderManagers
  103.      * @param PricingManagerLocatorInterface $pricingManagers
  104.      * @param PriceSystemLocatorInterface $priceSystems
  105.      * @param AvailabilitySystemLocatorInterface $availabilitySystems
  106.      * @param CheckoutManagerFactoryLocatorInterface $checkoutManagerFactories
  107.      * @param CommitOrderProcessorLocatorInterface $commitOrderProcessors
  108.      * @param FilterServiceLocatorInterface $filterServices
  109.      */
  110.     public function __construct(
  111.         ContainerInterface $container,
  112.         CartManagerLocatorInterface $cartManagers,
  113.         OrderManagerLocatorInterface $orderManagers,
  114.         PricingManagerLocatorInterface $pricingManagers,
  115.         PriceSystemLocatorInterface $priceSystems,
  116.         AvailabilitySystemLocatorInterface $availabilitySystems,
  117.         CheckoutManagerFactoryLocatorInterface $checkoutManagerFactories,
  118.         CommitOrderProcessorLocatorInterface $commitOrderProcessors,
  119.         FilterServiceLocatorInterface $filterServices
  120.     ) {
  121.         $this->container $container;
  122.         $this->cartManagers $cartManagers;
  123.         $this->orderManagers $orderManagers;
  124.         $this->pricingManagers $pricingManagers;
  125.         $this->priceSystems $priceSystems;
  126.         $this->availabilitySystems $availabilitySystems;
  127.         $this->checkoutManagerFactories $checkoutManagerFactories;
  128.         $this->commitOrderProcessors $commitOrderProcessors;
  129.         $this->filterServices $filterServices;
  130.     }
  131.     public static function getInstance(): self
  132.     {
  133.         return \Pimcore::getContainer()->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_FACTORY);
  134.     }
  135.     public function getEnvironment(): EnvironmentInterface
  136.     {
  137.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_ENVIRONMENT);
  138.     }
  139.     /**
  140.      * Returns cart manager for a specific tenant. If no tenant is passed it will fall back to the current
  141.      * checkout tenant or to "default" if no current checkout tenant is set.
  142.      *
  143.      * @param string|null $tenant
  144.      *
  145.      * @return CartManagerInterface
  146.      */
  147.     public function getCartManager(string $tenant null): CartManagerInterface
  148.     {
  149.         return $this->cartManagers->getCartManager($tenant);
  150.     }
  151.     /**
  152.      * Returns order manager for a specific tenant. If no tenant is passed it will fall back to the current
  153.      * checkout tenant or to "default" if no current checkout tenant is set.
  154.      *
  155.      * @param string|null $tenant
  156.      *
  157.      * @return OrderManagerInterface
  158.      */
  159.     public function getOrderManager(string $tenant null): OrderManagerInterface
  160.     {
  161.         return $this->orderManagers->getOrderManager($tenant);
  162.     }
  163.     /**
  164.      * Returns pricing manager for a specific tenant. If no tenant is passed it will fall back to the current
  165.      * checkout tenant or to "default" if no current checkout tenant is set.
  166.      *
  167.      * @param string|null $tenant
  168.      *
  169.      * @return PricingManagerInterface
  170.      */
  171.     public function getPricingManager(string $tenant null): PricingManagerInterface
  172.     {
  173.         return $this->pricingManagers->getPricingManager($tenant);
  174.     }
  175.     /**
  176.      * Returns a price system by name. Falls back to "default" if no name is passed.
  177.      *
  178.      * @param string|null $name
  179.      *
  180.      * @return PriceSystemInterface
  181.      */
  182.     public function getPriceSystem(string $name null): PriceSystemInterface
  183.     {
  184.         return $this->priceSystems->getPriceSystem($name);
  185.     }
  186.     /**
  187.      * Returns an availability system by name. Falls back to "default" if no name is passed.
  188.      *
  189.      * @param string|null $name
  190.      *
  191.      * @return AvailabilitySystemInterface
  192.      */
  193.     public function getAvailabilitySystem(string $name null): AvailabilitySystemInterface
  194.     {
  195.         return $this->availabilitySystems->getAvailabilitySystem($name);
  196.     }
  197.     /**
  198.      * Returns checkout manager for a specific tenant. If no tenant is passed it will fall back to the current
  199.      * checkout tenant or to "default" if no current checkout tenant is set.
  200.      *
  201.      * @param CartInterface $cart
  202.      * @param string|null $tenant
  203.      *
  204.      * @return CheckoutManagerInterface
  205.      */
  206.     public function getCheckoutManager(CartInterface $cartstring $tenant null): CheckoutManagerInterface
  207.     {
  208.         $factory $this->checkoutManagerFactories->getCheckoutManagerFactory($tenant);
  209.         return $factory->createCheckoutManager($cart);
  210.     }
  211.     /**
  212.      * Returns a commit order processor which is configured for a specific checkout manager
  213.      *
  214.      * @param string|null $tenant
  215.      *
  216.      * @return CommitOrderProcessorInterface
  217.      */
  218.     public function getCommitOrderProcessor(string $tenant null): CommitOrderProcessorInterface
  219.     {
  220.         return $this->commitOrderProcessors->getCommitOrderProcessor($tenant);
  221.     }
  222.     public function getPaymentManager(): PaymentManagerInterface
  223.     {
  224.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_PAYMENT_MANAGER);
  225.     }
  226.     /**
  227.      * Returns the index service which holds a collection of all index workers
  228.      *
  229.      * @return IndexService
  230.      */
  231.     public function getIndexService(): IndexService
  232.     {
  233.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_INDEX_SERVICE);
  234.     }
  235.     /**
  236.      * Returns the filter service for the currently set assortment tenant. Falls back to "default" if no tenant is passed
  237.      * and there is no current assortment tenant set.
  238.      *
  239.      * @param string|null $tenant
  240.      *
  241.      * @return FilterService
  242.      */
  243.     public function getFilterService(string $tenant null): FilterService
  244.     {
  245.         return $this->filterServices->getFilterService($tenant);
  246.     }
  247.     public function getAllTenants(): array
  248.     {
  249.         return $this->getIndexService()->getTenants();
  250.     }
  251.     public function getOfferToolService(): ServiceInterface
  252.     {
  253.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_OFFER_TOOL);
  254.     }
  255.     public function getVoucherService(): VoucherServiceInterface
  256.     {
  257.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_VOUCHER_SERVICE);
  258.     }
  259.     /**
  260.      * Builds a token manager for a specific token configuration
  261.      *
  262.      * @param AbstractVoucherTokenType $configuration
  263.      *
  264.      * @return TokenManagerInterface
  265.      */
  266.     public function getTokenManager(AbstractVoucherTokenType $configuration): TokenManagerInterface
  267.     {
  268.         $tokenManagerFactory $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_TOKEN_MANAGER_FACTORY);
  269.         return $tokenManagerFactory->getTokenManager($configuration);
  270.     }
  271.     public function getTrackingManager(): TrackingManagerInterface
  272.     {
  273.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_TRACKING_MANAGER);
  274.     }
  275.     public function saveState()
  276.     {
  277.         $this->getCartManager()->save();
  278.         $this->getEnvironment()->save();
  279.     }
  280. }