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

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.      * @var EnvironmentInterface
  49.      */
  50.     private $environment;
  51.     /**
  52.      * Tenant specific cart managers
  53.      *
  54.      * @var CartManagerLocatorInterface
  55.      */
  56.     private $cartManagers;
  57.     /**
  58.      * Tenant specific order managers
  59.      *
  60.      * @var OrderManagerLocatorInterface
  61.      */
  62.     private $orderManagers;
  63.     /**
  64.      * Pricing managers registered by tenant
  65.      *
  66.      * @var PricingManagerLocatorInterface
  67.      */
  68.     private $pricingManagers;
  69.     /**
  70.      * Price systems registered by name
  71.      *
  72.      * @var PriceSystemLocatorInterface
  73.      */
  74.     private $priceSystems;
  75.     /**
  76.      * Availability systems registered by name
  77.      *
  78.      * @var AvailabilitySystemLocatorInterface
  79.      */
  80.     private $availabilitySystems;
  81.     /**
  82.      * Checkout manager factories registered by tenant
  83.      *
  84.      * @var CheckoutManagerFactoryLocatorInterface
  85.      */
  86.     private $checkoutManagerFactories;
  87.     /**
  88.      * Commit order processors registered by tenant
  89.      *
  90.      * @var CommitOrderProcessorLocatorInterface
  91.      */
  92.     private $commitOrderProcessors;
  93.     /**
  94.      * Filter services registered by ^tenant
  95.      *
  96.      * @var FilterServiceLocatorInterface
  97.      */
  98.     private $filterServices;
  99.     /**
  100.      * Systems with multiple instances (e.g. price systems or tenant specific systems) are
  101.      * injected through a service locator which is indexed by tenant/name. All other services
  102.      * are loaded from the container on demand to make sure only services needed are built.
  103.      *
  104.      * @param ContainerInterface $container
  105.      * @param CartManagerLocatorInterface $cartManagers
  106.      * @param OrderManagerLocatorInterface $orderManagers
  107.      * @param PricingManagerLocatorInterface $pricingManagers
  108.      * @param PriceSystemLocatorInterface $priceSystems
  109.      * @param AvailabilitySystemLocatorInterface $availabilitySystems
  110.      * @param CheckoutManagerFactoryLocatorInterface $checkoutManagerFactories
  111.      * @param CommitOrderProcessorLocatorInterface $commitOrderProcessors
  112.      * @param FilterServiceLocatorInterface $filterServices
  113.      */
  114.     public function __construct(
  115.         ContainerInterface $container,
  116.         CartManagerLocatorInterface $cartManagers,
  117.         OrderManagerLocatorInterface $orderManagers,
  118.         PricingManagerLocatorInterface $pricingManagers,
  119.         PriceSystemLocatorInterface $priceSystems,
  120.         AvailabilitySystemLocatorInterface $availabilitySystems,
  121.         CheckoutManagerFactoryLocatorInterface $checkoutManagerFactories,
  122.         CommitOrderProcessorLocatorInterface $commitOrderProcessors,
  123.         FilterServiceLocatorInterface $filterServices
  124.     ) {
  125.         $this->container $container;
  126.         $this->cartManagers $cartManagers;
  127.         $this->orderManagers $orderManagers;
  128.         $this->pricingManagers $pricingManagers;
  129.         $this->priceSystems $priceSystems;
  130.         $this->availabilitySystems $availabilitySystems;
  131.         $this->checkoutManagerFactories $checkoutManagerFactories;
  132.         $this->commitOrderProcessors $commitOrderProcessors;
  133.         $this->filterServices $filterServices;
  134.     }
  135.     public static function getInstance(): self
  136.     {
  137.         return \Pimcore::getContainer()->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_FACTORY);
  138.     }
  139.     public function getEnvironment(): EnvironmentInterface
  140.     {
  141.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_ENVIRONMENT);
  142.     }
  143.     /**
  144.      * Returns cart manager for a specific tenant. If no tenant is passed it will fall back to the current
  145.      * checkout tenant or to "default" if no current checkout tenant is set.
  146.      *
  147.      * @param string|null $tenant
  148.      *
  149.      * @return CartManagerInterface
  150.      */
  151.     public function getCartManager(string $tenant null): CartManagerInterface
  152.     {
  153.         return $this->cartManagers->getCartManager($tenant);
  154.     }
  155.     /**
  156.      * Returns order manager for a specific tenant. If no tenant is passed it will fall back to the current
  157.      * checkout tenant or to "default" if no current checkout tenant is set.
  158.      *
  159.      * @param string|null $tenant
  160.      *
  161.      * @return OrderManagerInterface
  162.      */
  163.     public function getOrderManager(string $tenant null): OrderManagerInterface
  164.     {
  165.         return $this->orderManagers->getOrderManager($tenant);
  166.     }
  167.     /**
  168.      * Returns pricing manager for a specific tenant. If no tenant is passed it will fall back to the current
  169.      * checkout tenant or to "default" if no current checkout tenant is set.
  170.      *
  171.      * @param string|null $tenant
  172.      *
  173.      * @return PricingManagerInterface
  174.      */
  175.     public function getPricingManager(string $tenant null): PricingManagerInterface
  176.     {
  177.         return $this->pricingManagers->getPricingManager($tenant);
  178.     }
  179.     /**
  180.      * Returns a price system by name. Falls back to "default" if no name is passed.
  181.      *
  182.      * @param string|null $name
  183.      *
  184.      * @return PriceSystemInterface
  185.      */
  186.     public function getPriceSystem(string $name null): PriceSystemInterface
  187.     {
  188.         return $this->priceSystems->getPriceSystem($name);
  189.     }
  190.     /**
  191.      * Returns an availability system by name. Falls back to "default" if no name is passed.
  192.      *
  193.      * @param string|null $name
  194.      *
  195.      * @return AvailabilitySystemInterface
  196.      */
  197.     public function getAvailabilitySystem(string $name null): AvailabilitySystemInterface
  198.     {
  199.         return $this->availabilitySystems->getAvailabilitySystem($name);
  200.     }
  201.     /**
  202.      * Returns checkout manager for a specific tenant. If no tenant is passed it will fall back to the current
  203.      * checkout tenant or to "default" if no current checkout tenant is set.
  204.      *
  205.      * @param CartInterface $cart
  206.      * @param string|null $tenant
  207.      *
  208.      * @return CheckoutManagerInterface
  209.      */
  210.     public function getCheckoutManager(CartInterface $cartstring $tenant null): CheckoutManagerInterface
  211.     {
  212.         $factory $this->checkoutManagerFactories->getCheckoutManagerFactory($tenant);
  213.         return $factory->createCheckoutManager($cart);
  214.     }
  215.     /**
  216.      * Returns a commit order processor which is configured for a specific checkout manager
  217.      *
  218.      * @param string|null $tenant
  219.      *
  220.      * @return CommitOrderProcessorInterface
  221.      */
  222.     public function getCommitOrderProcessor(string $tenant null): CommitOrderProcessorInterface
  223.     {
  224.         return $this->commitOrderProcessors->getCommitOrderProcessor($tenant);
  225.     }
  226.     public function getPaymentManager(): PaymentManagerInterface
  227.     {
  228.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_PAYMENT_MANAGER);
  229.     }
  230.     /**
  231.      * Returns the index service which holds a collection of all index workers
  232.      *
  233.      * @return IndexService
  234.      */
  235.     public function getIndexService(): IndexService
  236.     {
  237.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_INDEX_SERVICE);
  238.     }
  239.     /**
  240.      * Returns the filter service for the currently set assortment tenant. Falls back to "default" if no tenant is passed
  241.      * and there is no current assortment tenant set.
  242.      *
  243.      * @param string|null $tenant
  244.      *
  245.      * @return FilterService
  246.      */
  247.     public function getFilterService(string $tenant null): FilterService
  248.     {
  249.         return $this->filterServices->getFilterService($tenant);
  250.     }
  251.     public function getAllTenants(): array
  252.     {
  253.         return $this->getIndexService()->getTenants();
  254.     }
  255.     public function getOfferToolService(): ServiceInterface
  256.     {
  257.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_OFFER_TOOL);
  258.     }
  259.     public function getVoucherService(): VoucherServiceInterface
  260.     {
  261.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_VOUCHER_SERVICE);
  262.     }
  263.     /**
  264.      * Builds a token manager for a specific token configuration
  265.      *
  266.      * @param AbstractVoucherTokenType $configuration
  267.      *
  268.      * @return TokenManagerInterface
  269.      */
  270.     public function getTokenManager(AbstractVoucherTokenType $configuration): TokenManagerInterface
  271.     {
  272.         $tokenManagerFactory $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_TOKEN_MANAGER_FACTORY);
  273.         return $tokenManagerFactory->getTokenManager($configuration);
  274.     }
  275.     public function getTrackingManager(): TrackingManagerInterface
  276.     {
  277.         return $this->container->get(PimcoreEcommerceFrameworkExtension::SERVICE_ID_TRACKING_MANAGER);
  278.     }
  279.     public function saveState()
  280.     {
  281.         $this->getCartManager()->save();
  282.         $this->environment->save();
  283.     }
  284. }