objectManager = $objectManager; $this->clientFactoryPool = $clientFactories; $this->clientOptionsPool = $clientOptions; $this->engineResolver = $engineResolver; } /** * Returns configured search engine * * @return string * @since 100.1.0 */ public function getCurrentEngine() { return $this->engineResolver->getCurrentSearchEngine(); } /** * Create client instance * * @param string $engine * @param array $data * @return ClientInterface * @since 100.1.0 */ public function create($engine = '', array $data = []) { $engine = $engine ?: $this->getCurrentEngine(); if (!isset($this->clientFactoryPool[$engine])) { throw new \LogicException( 'There is no such client factory: ' . $engine ); } $factoryClass = $this->clientFactoryPool[$engine]; $factory = $this->objectManager->create($factoryClass); if (!($factory instanceof ClientFactoryInterface)) { throw new \InvalidArgumentException( 'Client factory must implement \Magento\AdvancedSearch\Model\Client\ClientFactoryInterface' ); } $optionsClass = $this->clientOptionsPool[$engine]; $clientOptions = $this->objectManager->create($optionsClass); if (!($clientOptions instanceof ClientOptionsInterface)) { throw new \InvalidArgumentException( 'Client options must implement \Magento\AdvancedSearch\Model\Client\ClientInterface' ); } $client = $factory->create($clientOptions->prepareClientOptions($data)); return $client; } }