custom/plugins/IntediaDoofinderSW6/src/Doofinder/Api/Search.php line 60

Open in your IDE?
  1. <?php
  2. namespace Intedia\Doofinder\Doofinder\Api;
  3. use GuzzleHttp\Client;
  4. use Intedia\Doofinder\Core\Content\Settings\Service\SettingsHandler;
  5. use Psr\Log\LoggerInterface;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. class Search
  9. {
  10.     const API_VERSION '5';
  11.     const CLIENT_TIMEOUT 2.5// seconds
  12.     const CONFIG_PREFIX 'IntediaDoofinderSW6.config.';
  13.     const MAX_RESULTS 1500;
  14.     const PAGE_SIZE 100;
  15.     /** @var SystemConfigService */
  16.     protected $systemConfigService;
  17.     /** @var LoggerInterface */
  18.     protected $logger;
  19.     /** @var SettingsHandler $settingsHandler */
  20.     protected SettingsHandler $settingsHandler;
  21.     /** @var Client */
  22.     protected $client;
  23.     /** @var string */
  24.     protected $baseUrl;
  25.     /** @var string */
  26.     protected $apiKey;
  27.     /** @var string */
  28.     protected $apiZone;
  29.     /**
  30.      * Search constructor.
  31.      *
  32.      * @param SystemConfigService $systemConfigService
  33.      * @param LoggerInterface $logger
  34.      * @param SettingsHandler $settingsHandler
  35.      */
  36.     public function __construct(
  37.         SystemConfigService $systemConfigService,
  38.         LoggerInterface $logger,
  39.         SettingsHandler $settingsHandler
  40.     ) {
  41.         $this->logger              $logger;
  42.         $this->systemConfigService $systemConfigService;
  43.         $this->settingsHandler     $settingsHandler;
  44.         if ($this->initConfig()) {
  45.             $this->client = new Client([
  46.                 'base_uri' => $this->baseUrl,
  47.                 'timeout' => self::CLIENT_TIMEOUT
  48.             ]);
  49.         }
  50.     }
  51.     /**
  52.      * Initializes api with config values
  53.      * @param SalesChannelContext|null $context
  54.      * @return bool
  55.      */
  56.     protected function initConfig(?SalesChannelContext $context null): bool
  57.     {
  58.         if (($apiKey $this->getConfig('apiKey'$context)) && ($searchDomain $this->getConfig('searchDomain'$context))) {
  59.             $apiInfo explode('-'$apiKey);
  60.             if (count($apiInfo) != 2) {
  61.                 return false;
  62.             }
  63.             $this->apiKey  $apiInfo[1];
  64.             $this->apiZone $apiInfo[0];
  65.             $this->baseUrl sprintf("https://$searchDomain/%s/"$this->apiZoneself::API_VERSION);
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.     /**
  71.      * @param $term
  72.      * @param $context
  73.      * @return array
  74.      */
  75.     public function queryIds($term$context)
  76.     {
  77.         $this->context  $context;
  78.         $resultIds      = [];
  79.         $page           1;
  80.         $dfResponse     $this->queryPage($term$pageself::PAGE_SIZE);
  81.         $productsToLoad self::MAX_RESULTS;
  82.         while ($dfResponse) {
  83.             $dfResults $dfResponse['results'];
  84.             for ($i 0$i count($dfResults) && ($productsToLoad 0); $i++, --$productsToLoad) {
  85.                 if (array_key_exists('group_id'$dfResults[$i])) {
  86.                     $resultIds[$dfResults[$i]['id']] = $dfResults[$i]['group_id'];
  87.                 } else {
  88.                     $resultIds[] = $dfResults[$i]['id'];
  89.                 }
  90.             }
  91.             $dfResponse $page self::PAGE_SIZE $dfResponse['total'] && $productsToLoad $this->queryPage($term, ++$pageself::PAGE_SIZE) : null;
  92.         }
  93.         return $resultIds;
  94.     }
  95.     /**
  96.      * @param $term
  97.      * @param $page
  98.      * @param $rpp
  99.      * @return mixed|null
  100.      */
  101.     protected function queryPage($term$page$rpp)
  102.     {
  103.         try {
  104.             if ($this->client) {
  105.                 $doofinderLayer $this->settingsHandler->getDoofinderLayer(
  106.                     $this->settingsHandler->getDomain(
  107.                         $this->context->getDomainId()
  108.                     )
  109.                 );
  110.                 $response $this->client->request('GET''search',
  111.                     [
  112.                         'query' => [
  113.                             'hashid' => $doofinderLayer $doofinderLayer->getDooFinderHashId() : '',
  114.                             'query' => $term,
  115.                             'page' => $page,
  116.                             'rpp' => $rpp
  117.                         ],
  118.                         'headers' => [
  119.                             'Authorization' => 'Token ' $this->apiKey
  120.                         ]
  121.                     ]
  122.                 );
  123.                 if ($response->getStatusCode() === 200) {
  124.                     if ($this->getConfig('doofinderDebug')) {
  125.                         $this->logger->error(json_encode([
  126.                             'baseUrl'        => $this->baseUrl,
  127.                             'endpoint'       => 'search',
  128.                             'method'         => 'GET',
  129.                             'authorization'  => 'Token ' $this->apiKey,
  130.                             'query'          => json_encode([
  131.                                 'hashid' => $doofinderLayer $doofinderLayer->getDooFinderHashId() : '',
  132.                                 'query' => $term,
  133.                                 'page' => $page,
  134.                                 'rpp' => $rpp
  135.                             ]),
  136.                             'responseStatus' => $response->getStatusCode(),
  137.                             'responseBody'   => json_encode(\GuzzleHttp\json_decode($response->getBody(), true))
  138.                         ]));
  139.                     }
  140.                     return \GuzzleHttp\json_decode($response->getBody(), true);
  141.                 }
  142.             }
  143.         } catch (\Exception $e) {
  144.             $this->logger->error("Exception receiving results from doofinder: " $e->getMessage());
  145.         }
  146.         return null;
  147.     }
  148.     /**
  149.      * @param $configKey
  150.      * @param null $context
  151.      * @return mixed|null
  152.      */
  153.     protected function getConfig($configKey$context null)
  154.     {
  155.         try {
  156.             $pluginConfig $this->systemConfigService->getDomain(self::CONFIG_PREFIX$context $context->getSalesChannel()->getId() : nulltrue);
  157.             $configKey    self::CONFIG_PREFIX $configKey;
  158.             if ($pluginConfig && array_key_exists($configKey$pluginConfig)) {
  159.                 return $pluginConfig[$configKey];
  160.             }
  161.         }
  162.         catch (\Exception $e) {
  163.             $this->logger->error($e->getMessage() . PHP_EOL $e->getTraceAsString());
  164.         }
  165.         return null;
  166.     }
  167. }