vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/IndexService/Config/AbstractConfig.php line 350

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\IndexService\Config;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Config\Definition\Attribute;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\Worker\WorkerInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractCategory;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\IndexableInterface;
  19. use Pimcore\Model\DataObject;
  20. abstract class AbstractConfig implements ConfigInterface
  21. {
  22.     /**
  23.      * @var string
  24.      */
  25.     protected $tenantName;
  26.     /**
  27.      * @var array
  28.      */
  29.     protected $attributeConfig;
  30.     /**
  31.      * @var array
  32.      */
  33.     protected $searchAttributeConfig;
  34.     /**
  35.      * @var AttributeFactory
  36.      */
  37.     protected $attributeFactory;
  38.     /**
  39.      * @var Attribute[]
  40.      */
  41.     protected $attributes;
  42.     /**
  43.      * @var array
  44.      */
  45.     protected $searchAttributes;
  46.     /**
  47.      * @var array
  48.      */
  49.     protected $filterTypes;
  50.     /**
  51.      * @var WorkerInterface
  52.      */
  53.     protected $tenantWorker;
  54.     /**
  55.      * @var array|null
  56.      */
  57.     protected $filterTypeConfig;
  58.     /**
  59.      * @var array
  60.      */
  61.     protected $options;
  62.     /**
  63.      * @param string $tenantName
  64.      * @param array[]|Attribute[] $attributes
  65.      * @param array $searchAttributes
  66.      * @param array $filterTypes
  67.      * @param array $options
  68.      */
  69.     public function __construct(
  70.         string $tenantName,
  71.         array $attributes,
  72.         array $searchAttributes,
  73.         array $filterTypes,
  74.         array $options = []
  75.     ) {
  76.         $this->tenantName $tenantName;
  77.         $this->attributeConfig $attributes;
  78.         $this->searchAttributeConfig $searchAttributes;
  79.         $this->filterTypes $filterTypes;
  80.         $this->processOptions($options);
  81.     }
  82.     /**
  83.      * Attribute configuration
  84.      *
  85.      * @return array
  86.      */
  87.     public function getAttributeConfig()
  88.     {
  89.         return $this->attributeConfig;
  90.     }
  91.     /**
  92.      * Sets attribute factory as dependency. This was added as setter for BC reasons and will be added to the constructor
  93.      * signature in Pimcore 10.
  94.      *
  95.      * TODO Pimcore 10 add to constructor signature.
  96.      *
  97.      * @required
  98.      *
  99.      * @param AttributeFactory $attributeFactory
  100.      */
  101.     public function setAttributeFactory(AttributeFactory $attributeFactory)
  102.     {
  103.         if (null !== $this->attributeFactory) {
  104.             throw new \RuntimeException('Attribute factory is already set.');
  105.         }
  106.         $this->attributeFactory $attributeFactory;
  107.         $this->attributes = [];
  108.         $this->searchAttributes = [];
  109.         $this->buildAttributes($this->attributeConfig);
  110.         foreach ($this->searchAttributeConfig as $searchAttribute) {
  111.             $this->addSearchAttribute($searchAttribute);
  112.         }
  113.     }
  114.     protected function buildAttributes(array $attributes)
  115.     {
  116.         foreach ($attributes as $attribute) {
  117.             if ($attribute instanceof Attribute) {
  118.                 $this->addAttribute($attribute);
  119.             } elseif (is_array($attribute)) {
  120.                 $attribute $this->attributeFactory->createAttribute($attribute);
  121.                 $this->addAttribute($attribute);
  122.             } else {
  123.                 throw new \InvalidArgumentException(sprintf(
  124.                     'Wrong type for attribute. Expected Attribute or array, got "%s"',
  125.                     is_object($attribute) ? get_class($attribute) : gettype($attribute)
  126.                 ));
  127.             }
  128.         }
  129.     }
  130.     protected function addAttribute(Attribute $attribute)
  131.     {
  132.         $this->attributes[$attribute->getName()] = $attribute;
  133.     }
  134.     protected function addSearchAttribute(string $searchAttribute)
  135.     {
  136.         if (!isset($this->attributes[$searchAttribute])) {
  137.             throw new \InvalidArgumentException(sprintf(
  138.                 'The search attribute "%s" in product index tenant "%s" is not defined as attribute',
  139.                 $searchAttribute,
  140.                 $this->tenantName
  141.             ));
  142.         }
  143.         $this->searchAttributes[] = $searchAttribute;
  144.     }
  145.     protected function processOptions(array $options)
  146.     {
  147.         // noop - to implemented by configs supporting options
  148.     }
  149.     /**
  150.      * {@inheritdoc}
  151.      */
  152.     public function setTenantWorker(WorkerInterface $tenantWorker)
  153.     {
  154.         $this->checkTenantWorker($tenantWorker);
  155.         $this->tenantWorker $tenantWorker;
  156.     }
  157.     /**
  158.      * Checks if tenant worker matches prerequisites (config wrapped in worker is this instance and instance has no
  159.      * worker set yet).
  160.      *
  161.      * @param WorkerInterface $tenantWorker
  162.      */
  163.     protected function checkTenantWorker(WorkerInterface $tenantWorker)
  164.     {
  165.         if (null !== $this->tenantWorker) {
  166.             throw new \LogicException(sprintf('Worker for tenant "%s" is already set'$this->tenantName));
  167.         }
  168.         // make sure the worker is the one working on this config instance
  169.         if ($tenantWorker->getTenantConfig() !== $this) {
  170.             throw new \LogicException('Worker config does not match the config the worker is about to be set to');
  171.         }
  172.     }
  173.     /**
  174.      * {@inheritdoc}
  175.      */
  176.     public function getTenantWorker()
  177.     {
  178.         // the worker is expected to call setTenantWorker as soon as possible
  179.         if (null === $this->tenantWorker) {
  180.             throw new \RuntimeException('Tenant worker is not set.');
  181.         }
  182.         return $this->tenantWorker;
  183.     }
  184.     /**
  185.      * @return string
  186.      */
  187.     public function getTenantName()
  188.     {
  189.         return $this->tenantName;
  190.     }
  191.     /**
  192.      * Returns configured attributes for product index
  193.      *
  194.      * @return Attribute[]
  195.      */
  196.     public function getAttributes(): array
  197.     {
  198.         // TODO Pimcore 10 remove as soon as attribute factory was added to the constructor.
  199.         if (null === $this->attributes) {
  200.             throw new \RuntimeException('Attributes are not built yet. Is the service properly configured to set an attribute factory?');
  201.         }
  202.         return $this->attributes;
  203.     }
  204.     /**
  205.      * Returns full text search index attribute names for product index
  206.      *
  207.      * @return array
  208.      */
  209.     public function getSearchAttributes(): array
  210.     {
  211.         // TODO Pimcore 10 remove as soon as attribute factory was added to the constructor.
  212.         if (null === $this->attributes) {
  213.             throw new \RuntimeException('Search attributes are not built yet. Is the service properly configured to set an attribute factory?');
  214.         }
  215.         return $this->searchAttributes;
  216.     }
  217.     /**
  218.      * return all supported filter types for product index
  219.      *
  220.      * @return array|null
  221.      */
  222.     public function getFilterTypeConfig()
  223.     {
  224.         return $this->filterTypeConfig;
  225.     }
  226.     /**
  227.      * @param IndexableInterface $object
  228.      *
  229.      * @return bool
  230.      */
  231.     public function isActive(IndexableInterface $object)
  232.     {
  233.         return true;
  234.     }
  235.     /**
  236.      * @param IndexableInterface $object
  237.      * @param int|null $subObjectId
  238.      *
  239.      * @return AbstractCategory[]
  240.      */
  241.     public function getCategories(IndexableInterface $object$subObjectId null)
  242.     {
  243.         return $object->getCategories();
  244.     }
  245.     /**
  246.      * creates an array of sub ids for the given object
  247.      * use that function, if one object should be indexed more than once (e.g. if field collections are in use)
  248.      *
  249.      * @param IndexableInterface $object
  250.      *
  251.      * @return IndexableInterface[]
  252.      */
  253.     public function createSubIdsForObject(IndexableInterface $object)
  254.     {
  255.         return [$object->getId() => $object];
  256.     }
  257.     /**
  258.      * checks if there are some zombie subIds around and returns them for cleanup
  259.      *
  260.      * @param IndexableInterface $object
  261.      * @param array $subIds
  262.      *
  263.      * @return array
  264.      */
  265.     public function getSubIdsToCleanup(IndexableInterface $object, array $subIds)
  266.     {
  267.         return [];
  268.     }
  269.     /**
  270.      * creates virtual parent id for given sub id
  271.      * default is getOSParentId
  272.      *
  273.      * @param IndexableInterface $object
  274.      * @param int $subId
  275.      *
  276.      * @return int|string|null
  277.      */
  278.     public function createVirtualParentIdForSubId(IndexableInterface $object$subId)
  279.     {
  280.         return $object->getOSParentId();
  281.     }
  282.     /**
  283.      * Gets object by id, can consider subIds and therefore return e.g. an array of values
  284.      * always returns object itself - see also getObjectMockupById
  285.      *
  286.      * @param int $objectId
  287.      * @param bool $onlyMainObject - only returns main object
  288.      *
  289.      * @return DataObject|null
  290.      */
  291.     public function getObjectById($objectId$onlyMainObject false)
  292.     {
  293.         return DataObject::getById($objectId);
  294.     }
  295.     /**
  296.      * Gets object mockup by id, can consider subIds and therefore return e.g. an array of values
  297.      * always returns a object mockup if available
  298.      *
  299.      * @param int $objectId
  300.      *
  301.      * @return IndexableInterface|null
  302.      */
  303.     public function getObjectMockupById($objectId)
  304.     {
  305.         $object $this->getObjectById($objectId);
  306.         if ($object instanceof IndexableInterface) {
  307.             return $object;
  308.         }
  309.         return null;
  310.     }
  311.     /**
  312.      * returns column type for id
  313.      *
  314.      * @param bool $isPrimary
  315.      *
  316.      * @return string
  317.      */
  318.     public function getIdColumnType($isPrimary)
  319.     {
  320.         if ($isPrimary) {
  321.             return "int(11) NOT NULL default '0'";
  322.         } else {
  323.             return 'int(11) NOT NULL';
  324.         }
  325.     }
  326. }