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

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