Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClientResolver.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\ObjectManagerInterface;
10 
16 {
24  protected $scopeConfig;
25 
32  protected $objectManager;
33 
39  private $clientFactoryPool;
40 
46  private $clientOptionsPool;
47 
51  private $engineResolver;
52 
60  protected $path;
61 
67  protected $scope;
68 
75  public function __construct(
77  array $clientFactories,
78  array $clientOptions,
79  EngineResolverInterface $engineResolver
80  ) {
81  $this->objectManager = $objectManager;
82  $this->clientFactoryPool = $clientFactories;
83  $this->clientOptionsPool = $clientOptions;
84  $this->engineResolver = $engineResolver;
85  }
86 
93  public function getCurrentEngine()
94  {
95  return $this->engineResolver->getCurrentSearchEngine();
96  }
97 
106  public function create($engine = '', array $data = [])
107  {
108  $engine = $engine ?: $this->getCurrentEngine();
109 
110  if (!isset($this->clientFactoryPool[$engine])) {
111  throw new \LogicException(
112  'There is no such client factory: ' . $engine
113  );
114  }
115  $factoryClass = $this->clientFactoryPool[$engine];
116  $factory = $this->objectManager->create($factoryClass);
117  if (!($factory instanceof ClientFactoryInterface)) {
118  throw new \InvalidArgumentException(
119  'Client factory must implement \Magento\AdvancedSearch\Model\Client\ClientFactoryInterface'
120  );
121  }
122 
123  $optionsClass = $this->clientOptionsPool[$engine];
124  $clientOptions = $this->objectManager->create($optionsClass);
125  if (!($clientOptions instanceof ClientOptionsInterface)) {
126  throw new \InvalidArgumentException(
127  'Client options must implement \Magento\AdvancedSearch\Model\Client\ClientInterface'
128  );
129  }
130 
131  $client = $factory->create($clientOptions->prepareClientOptions($data));
132 
133  return $client;
134  }
135 }
__construct(ObjectManagerInterface $objectManager, array $clientFactories, array $clientOptions, EngineResolverInterface $engineResolver)