vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/IndexService/IndexService.php line 291

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 Enterprise License (PEL)
  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 PEL
  14.  */
  15. namespace Pimcore\Bundle\EcommerceFrameworkBundle\IndexService;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\Exception\InvalidConfigException;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\ConfigInterface;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Exception\DefaultWorkerNotFoundException;
  20. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Exception\WorkerNotFoundException;
  21. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  22. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\WorkerInterface;
  23. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\IndexableInterface;
  24. class IndexService
  25. {
  26.     /**
  27.      * @var EnvironmentInterface
  28.      */
  29.     protected $environment;
  30.     /**
  31.      * @var WorkerInterface[]
  32.      */
  33.     protected $tenantWorkers = [];
  34.     /**
  35.      * @var string
  36.      */
  37.     protected $defaultTenant 'default';
  38.     /**
  39.      * @param EnvironmentInterface $environment
  40.      * @param WorkerInterface[] $tenantWorkers
  41.      * @param string $defaultTenant
  42.      */
  43.     public function __construct(EnvironmentInterface $environment, array $tenantWorkers = [], string $defaultTenant 'default')
  44.     {
  45.         $this->environment $environment;
  46.         foreach ($tenantWorkers as $tenantWorker) {
  47.             $this->registerTenantWorker($tenantWorker);
  48.         }
  49.         if (null !== $defaultTenant && !empty($defaultTenant)) {
  50.             $this->defaultTenant $defaultTenant;
  51.         }
  52.     }
  53.     protected function registerTenantWorker(WorkerInterface $tenantWorker)
  54.     {
  55.         $this->tenantWorkers[$tenantWorker->getTenantConfig()->getTenantName()] = $tenantWorker;
  56.     }
  57.     public function getTenants(): array
  58.     {
  59.         return array_keys($this->tenantWorkers);
  60.     }
  61.     /**
  62.      * Returns a specific tenant worker
  63.      *
  64.      * @param string $tenant
  65.      *
  66.      * @return WorkerInterface
  67.      *
  68.      * @throws WorkerNotFoundException
  69.      */
  70.     public function getTenantWorker(string $tenant): WorkerInterface
  71.     {
  72.         if (!array_key_exists($tenant$this->tenantWorkers)) {
  73.             throw new WorkerNotFoundException(sprintf('Tenant "%s" doesn\'t exist'$tenant));
  74.         }
  75.         return $this->tenantWorkers[$tenant];
  76.     }
  77.     /**
  78.      * Returns default worker as set in defaultTenant
  79.      *
  80.      * @return WorkerInterface
  81.      *
  82.      * @throws DefaultWorkerNotFoundException
  83.      */
  84.     public function getDefaultWorker(): WorkerInterface
  85.     {
  86.         if (!array_key_exists($this->defaultTenant$this->tenantWorkers)) {
  87.             throw new DefaultWorkerNotFoundException(sprintf(
  88.                 'Could not load default worker as there is no worker registered for the tenant "%s"',
  89.                 $this->defaultTenant
  90.             ));
  91.         }
  92.         return $this->tenantWorkers[$this->defaultTenant];
  93.     }
  94.     /**
  95.      * @deprecated
  96.      *
  97.      * @param string|null $tenant
  98.      *
  99.      * @return array
  100.      */
  101.     public function getGeneralSearchColumns(string $tenant null)
  102.     {
  103.         return $this->getGeneralSearchAttributes($tenant);
  104.     }
  105.     /**
  106.      * Returns all attributes marked as general search attributes for full text search
  107.      *
  108.      * @param string $tenant
  109.      *
  110.      * @return array
  111.      *
  112.      * @throws InvalidConfigException
  113.      */
  114.     public function getGeneralSearchAttributes(string $tenant null): array
  115.     {
  116.         try {
  117.             $tenantWorker $this->resolveTenantWorker($tenant);
  118.             return $tenantWorker->getGeneralSearchAttributes();
  119.         } catch (DefaultWorkerNotFoundException $e) {
  120.             return [];
  121.         }
  122.     }
  123.     /**
  124.      * @deprecated
  125.      */
  126.     public function createOrUpdateTable()
  127.     {
  128.         $this->createOrUpdateIndexStructures();
  129.     }
  130.     /**
  131.      * Creates or updates necessary index structures (e.g. database tables)
  132.      */
  133.     public function createOrUpdateIndexStructures()
  134.     {
  135.         foreach ($this->tenantWorkers as $tenant => $tenantWorker) {
  136.             $tenantWorker->createOrUpdateIndexStructures();
  137.         }
  138.     }
  139.     /**
  140.      * Deletes given element from index
  141.      *
  142.      * @param IndexableInterface $object
  143.      */
  144.     public function deleteFromIndex(IndexableInterface $object)
  145.     {
  146.         foreach ($this->tenantWorkers as $tenant => $tenantWorker) {
  147.             $tenantWorker->deleteFromIndex($object);
  148.         }
  149.     }
  150.     /**
  151.      * Updates given element in index
  152.      *
  153.      * @param IndexableInterface $object
  154.      */
  155.     public function updateIndex(IndexableInterface $object)
  156.     {
  157.         foreach ($this->tenantWorkers as $tenant => $tenantWorker) {
  158.             $tenantWorker->updateIndex($object);
  159.         }
  160.     }
  161.     /**
  162.      * Returns all index attributes
  163.      *
  164.      * @param bool $considerHideInFieldList
  165.      * @param string|null $tenant
  166.      *
  167.      * @return array
  168.      *
  169.      * @throws InvalidConfigException
  170.      */
  171.     public function getIndexAttributes(bool $considerHideInFieldList falsestring $tenant null): array
  172.     {
  173.         try {
  174.             $tenantWorker $this->resolveTenantWorker($tenant);
  175.             return $tenantWorker->getIndexAttributes($considerHideInFieldList);
  176.         } catch (DefaultWorkerNotFoundException $e) {
  177.             return [];
  178.         }
  179.     }
  180.     /**
  181.      * @deprecated
  182.      *
  183.      * @param bool $considerHideInFieldList
  184.      * @param string|null $tenant
  185.      *
  186.      * @return mixed
  187.      *
  188.      * @throws InvalidConfigException
  189.      */
  190.     public function getIndexColumns($considerHideInFieldList false$tenant null)
  191.     {
  192.         return $this->getIndexAttributes($considerHideInFieldList$tenant);
  193.     }
  194.     /**
  195.      * Returns all filter groups
  196.      *
  197.      * @param string $tenant
  198.      *
  199.      * @return array
  200.      *
  201.      * @throws InvalidConfigException
  202.      */
  203.     public function getAllFilterGroups(string $tenant null): array
  204.     {
  205.         try {
  206.             $tenantWorker $this->resolveTenantWorker($tenant);
  207.             return $tenantWorker->getAllFilterGroups();
  208.         } catch (DefaultWorkerNotFoundException $e) {
  209.             return [];
  210.         }
  211.     }
  212.     /**
  213.      * Returns all index attributes for a given filter group
  214.      *
  215.      * @param string $filterType
  216.      * @param string|null $tenant
  217.      *
  218.      * @return array
  219.      *
  220.      * @throws InvalidConfigException
  221.      */
  222.     public function getIndexAttributesByFilterGroup($filterTypestring $tenant null): array
  223.     {
  224.         try {
  225.             $tenantWorker $this->resolveTenantWorker($tenant);
  226.             return $tenantWorker->getIndexAttributesByFilterGroup($filterType);
  227.         } catch (DefaultWorkerNotFoundException $e) {
  228.             return [];
  229.         }
  230.     }
  231.     /**
  232.      * @deprecated
  233.      *
  234.      * @param string $filterType
  235.      * @param string|null $tenant
  236.      *
  237.      * @return mixed
  238.      *
  239.      * @throws InvalidConfigException
  240.      */
  241.     public function getIndexColumnsByFilterGroup($filterType$tenant null)
  242.     {
  243.         return $this->getIndexAttributesByFilterGroup($filterType$tenant);
  244.     }
  245.     /**
  246.      * Returns current tenant configuration
  247.      *
  248.      * @return ConfigInterface
  249.      *
  250.      * @throws InvalidConfigException
  251.      */
  252.     public function getCurrentTenantConfig()
  253.     {
  254.         return $this->getCurrentTenantWorker()->getTenantConfig();
  255.     }
  256.     public function getCurrentTenantWorker(): WorkerInterface
  257.     {
  258.         return $this->resolveTenantWorker();
  259.     }
  260.     public function getProductListForCurrentTenant(): ProductListInterface
  261.     {
  262.         $tenantWorker $this->getCurrentTenantWorker();
  263.         return $tenantWorker->getProductList();
  264.     }
  265.     public function getProductListForTenant(string $tenant): ProductListInterface
  266.     {
  267.         $tenantWorker $this->resolveTenantWorker($tenant);
  268.         return $tenantWorker->getProductList();
  269.     }
  270.     /**
  271.      * Resolve tenant worker either from given tenant name or from the current tenant
  272.      *
  273.      * @param string|null $tenant
  274.      *
  275.      * @return WorkerInterface
  276.      *
  277.      * @throws WorkerNotFoundException
  278.      */
  279.     protected function resolveTenantWorker(string $tenant null): WorkerInterface
  280.     {
  281.         if (null === $tenant) {
  282.             $tenant $this->environment->getCurrentAssortmentTenant();
  283.         }
  284.         if ($tenant) {
  285.             if (!array_key_exists($tenant$this->tenantWorkers)) {
  286.                 throw new WorkerNotFoundException(sprintf('Tenant "%s" doesn\'t exist'$tenant));
  287.             }
  288.             return $this->tenantWorkers[$tenant];
  289.         }
  290.         return $this->getDefaultWorker();
  291.     }
  292.     /**
  293.      * @param WorkerInterface[] $tenantWorkers
  294.      *
  295.      * @return IndexService
  296.      */
  297.     public function setTenantWorkers(array $tenantWorkers): self
  298.     {
  299.         $tenantWorkerAssocList = [];
  300.         foreach ($tenantWorkers as $tenantWorker) {
  301.             $tenantWorkerAssocList[$tenantWorker->getTenantConfig()->getTenantName()] = $tenantWorker;
  302.         }
  303.         $this->tenantWorkers $tenantWorkerAssocList;
  304.         return $this;
  305.     }
  306. }