|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CatalogSearch\Model\Indexer; |
| 7 | + |
| 8 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 9 | +use Magento\Framework\Indexer\IndexStructureInterface; |
| 10 | +use Magento\Framework\ObjectManagerInterface; |
| 11 | +use Magento\Store\Model\ScopeInterface; |
| 12 | + |
| 13 | +class IndexStructureFactory |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Object Manager instance |
| 17 | + * |
| 18 | + * @var ObjectManagerInterface |
| 19 | + */ |
| 20 | + protected $objectManager = null; |
| 21 | + |
| 22 | + /** |
| 23 | + * Instance name to create |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $structures = null; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var ScopeConfigInterface |
| 31 | + */ |
| 32 | + private $scopeConfig; |
| 33 | + |
| 34 | + /** |
| 35 | + * Configuration path by which current indexer handler stored |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + private $configPath; |
| 40 | + |
| 41 | + /** |
| 42 | + * Factory constructor |
| 43 | + * |
| 44 | + * @param ObjectManagerInterface $objectManager |
| 45 | + * @param ScopeConfigInterface $scopeConfig |
| 46 | + * @param string $configPath |
| 47 | + * @param string[] $structures |
| 48 | + */ |
| 49 | + public function __construct( |
| 50 | + ObjectManagerInterface $objectManager, |
| 51 | + ScopeConfigInterface $scopeConfig, |
| 52 | + $configPath, |
| 53 | + array $structures = [] |
| 54 | + ) { |
| 55 | + $this->objectManager = $objectManager; |
| 56 | + $this->scopeConfig = $scopeConfig; |
| 57 | + $this->configPath = $configPath; |
| 58 | + $this->structures = $structures; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Create index structure |
| 63 | + * |
| 64 | + * @param array $data |
| 65 | + * @return IndexStructureInterface |
| 66 | + */ |
| 67 | + public function create(array $data = []) |
| 68 | + { |
| 69 | + $currentStructure = $this->scopeConfig->getValue($this->configPath, ScopeInterface::SCOPE_STORE); |
| 70 | + if (!isset($this->structures[$currentStructure])) { |
| 71 | + throw new \LogicException( |
| 72 | + 'There is no such index structure: ' . $currentStructure |
| 73 | + ); |
| 74 | + } |
| 75 | + $indexStructure = $this->objectManager->create($this->structures[$currentStructure], $data); |
| 76 | + |
| 77 | + if (!$indexStructure instanceof IndexStructureInterface) { |
| 78 | + throw new \InvalidArgumentException( |
| 79 | + $indexStructure . ' doesn\'t implement \Magento\Framework\Indexer\IndexStructureInterface' |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + return $indexStructure; |
| 84 | + } |
| 85 | +} |
0 commit comments