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

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\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.     /**
  54.      * @param WorkerInterface $tenantWorker
  55.      *
  56.      * @internal
  57.      */
  58.     protected function registerTenantWorker(WorkerInterface $tenantWorker)
  59.     {
  60.         $this->tenantWorkers[$tenantWorker->getTenantConfig()->getTenantName()] = $tenantWorker;
  61.     }
  62.     public function getTenants(): array
  63.     {
  64.         return array_keys($this->tenantWorkers);
  65.     }
  66.     /**
  67.      * Returns a specific tenant worker
  68.      *
  69.      * @param string $tenant
  70.      *
  71.      * @return WorkerInterface
  72.      *
  73.      * @throws WorkerNotFoundException
  74.      */
  75.     public function getTenantWorker(string $tenant): WorkerInterface
  76.     {
  77.         if (!array_key_exists($tenant$this->tenantWorkers)) {
  78.             throw new WorkerNotFoundException(sprintf('Tenant "%s" doesn\'t exist'$tenant));
  79.         }
  80.         return $this->tenantWorkers[$tenant];
  81.     }
  82.     /**
  83.      * Returns default worker as set in defaultTenant
  84.      *
  85.      * @return WorkerInterface
  86.      *
  87.      * @throws DefaultWorkerNotFoundException
  88.      */
  89.     public function getDefaultWorker(): WorkerInterface
  90.     {
  91.         if (!array_key_exists($this->defaultTenant$this->tenantWorkers)) {
  92.             throw new DefaultWorkerNotFoundException(sprintf(
  93.                 'Could not load default worker as there is no worker registered for the tenant "%s"',
  94.                 $this->defaultTenant
  95.             ));
  96.         }
  97.         return $this->tenantWorkers[$this->defaultTenant];
  98.     }
  99.     /**
  100.      * Returns all attributes marked as general search attributes for full text search
  101.      *
  102.      * @param string|null $tenant
  103.      *
  104.      * @return array
  105.      *
  106.      * @throws InvalidConfigException
  107.      */
  108.     public function getGeneralSearchAttributes(string $tenant null): array
  109.     {
  110.         try {
  111.             $tenantWorker $this->resolveTenantWorker($tenant);
  112.             return $tenantWorker->getGeneralSearchAttributes();
  113.         } catch (DefaultWorkerNotFoundException $e) {
  114.             return [];
  115.         }
  116.     }
  117.     /**
  118.      * Creates or updates necessary index structures (e.g. database tables)
  119.      */
  120.     public function createOrUpdateIndexStructures()
  121.     {
  122.         foreach ($this->tenantWorkers as $tenant => $tenantWorker) {
  123.             $tenantWorker->createOrUpdateIndexStructures();
  124.         }
  125.     }
  126.     /**
  127.      * Deletes given element from index
  128.      *
  129.      * @param IndexableInterface $object
  130.      */
  131.     public function deleteFromIndex(IndexableInterface $object)
  132.     {
  133.         foreach ($this->tenantWorkers as $tenant => $tenantWorker) {
  134.             $tenantWorker->deleteFromIndex($object);
  135.         }
  136.     }
  137.     /**
  138.      * Updates given element in index
  139.      *
  140.      * @param IndexableInterface $object
  141.      */
  142.     public function updateIndex(IndexableInterface $object)
  143.     {
  144.         foreach ($this->tenantWorkers as $tenant => $tenantWorker) {
  145.             $tenantWorker->updateIndex($object);
  146.         }
  147.     }
  148.     /**
  149.      * Returns all index attributes
  150.      *
  151.      * @param bool $considerHideInFieldList
  152.      * @param string|null $tenant
  153.      *
  154.      * @return array
  155.      *
  156.      * @throws InvalidConfigException
  157.      */
  158.     public function getIndexAttributes(bool $considerHideInFieldList falsestring $tenant null): array
  159.     {
  160.         try {
  161.             $tenantWorker $this->resolveTenantWorker($tenant);
  162.             return $tenantWorker->getIndexAttributes($considerHideInFieldList);
  163.         } catch (DefaultWorkerNotFoundException $e) {
  164.             return [];
  165.         }
  166.     }
  167.     /**
  168.      * Returns all filter groups
  169.      *
  170.      * @param string|null $tenant
  171.      *
  172.      * @return array
  173.      *
  174.      * @throws InvalidConfigException
  175.      */
  176.     public function getAllFilterGroups(string $tenant null): array
  177.     {
  178.         try {
  179.             $tenantWorker $this->resolveTenantWorker($tenant);
  180.             return $tenantWorker->getAllFilterGroups();
  181.         } catch (DefaultWorkerNotFoundException $e) {
  182.             return [];
  183.         }
  184.     }
  185.     /**
  186.      * Returns all index attributes for a given filter group
  187.      *
  188.      * @param string $filterType
  189.      * @param string|null $tenant
  190.      *
  191.      * @return array
  192.      *
  193.      * @throws InvalidConfigException
  194.      */
  195.     public function getIndexAttributesByFilterGroup($filterTypestring $tenant null): array
  196.     {
  197.         try {
  198.             $tenantWorker $this->resolveTenantWorker($tenant);
  199.             return $tenantWorker->getIndexAttributesByFilterGroup($filterType);
  200.         } catch (DefaultWorkerNotFoundException $e) {
  201.             return [];
  202.         }
  203.     }
  204.     /**
  205.      * Returns current tenant configuration
  206.      *
  207.      * @return ConfigInterface
  208.      *
  209.      * @throws InvalidConfigException
  210.      */
  211.     public function getCurrentTenantConfig()
  212.     {
  213.         return $this->getCurrentTenantWorker()->getTenantConfig();
  214.     }
  215.     public function getCurrentTenantWorker(): WorkerInterface
  216.     {
  217.         return $this->resolveTenantWorker();
  218.     }
  219.     public function getProductListForCurrentTenant(): ProductListInterface
  220.     {
  221.         $tenantWorker $this->getCurrentTenantWorker();
  222.         return $tenantWorker->getProductList();
  223.     }
  224.     public function getProductListForTenant(string $tenant): ProductListInterface
  225.     {
  226.         $tenantWorker $this->resolveTenantWorker($tenant);
  227.         return $tenantWorker->getProductList();
  228.     }
  229.     /**
  230.      * Resolve tenant worker either from given tenant name or from the current tenant
  231.      *
  232.      * @param string|null $tenant
  233.      *
  234.      * @return WorkerInterface
  235.      *
  236.      * @throws WorkerNotFoundException
  237.      */
  238.     protected function resolveTenantWorker(string $tenant null): WorkerInterface
  239.     {
  240.         if (null === $tenant) {
  241.             $tenant $this->environment->getCurrentAssortmentTenant();
  242.         }
  243.         if ($tenant) {
  244.             if (!array_key_exists($tenant$this->tenantWorkers)) {
  245.                 throw new WorkerNotFoundException(sprintf('Tenant "%s" doesn\'t exist'$tenant));
  246.             }
  247.             return $this->tenantWorkers[$tenant];
  248.         }
  249.         return $this->getDefaultWorker();
  250.     }
  251.     /**
  252.      * @param WorkerInterface[] $tenantWorkers
  253.      *
  254.      * @return IndexService
  255.      */
  256.     public function setTenantWorkers(array $tenantWorkers): self
  257.     {
  258.         $tenantWorkerAssocList = [];
  259.         foreach ($tenantWorkers as $tenantWorker) {
  260.             $tenantWorkerAssocList[$tenantWorker->getTenantConfig()->getTenantName()] = $tenantWorker;
  261.         }
  262.         $this->tenantWorkers $tenantWorkerAssocList;
  263.         return $this;
  264.     }
  265. }