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

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