|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Config\PersonalizationConfig; |
| 6 | +use Algolia\AlgoliaSearch\RequestOptions\RequestOptions; |
| 7 | +use Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper; |
| 8 | +use Algolia\AlgoliaSearch\RetryStrategy\ClusterHosts; |
| 9 | + |
| 10 | +final class PersonalizationClient |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var \Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper |
| 14 | + */ |
| 15 | + private $api; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var \Algolia\AlgoliaSearch\Config\PersonalizationConfig |
| 19 | + */ |
| 20 | + private $config; |
| 21 | + |
| 22 | + /** |
| 23 | + * RecommendationClient constructor. |
| 24 | + */ |
| 25 | + public function __construct(ApiWrapper $api, PersonalizationConfig $config) |
| 26 | + { |
| 27 | + $this->api = $api; |
| 28 | + $this->config = $config; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param string|null $appId |
| 33 | + * @param string|null $apiKey |
| 34 | + * @param string|null $region |
| 35 | + * |
| 36 | + * @return PersonalizationClient |
| 37 | + */ |
| 38 | + public static function create($appId = null, $apiKey = null, $region = null) |
| 39 | + { |
| 40 | + $config = PersonalizationConfig::create($appId, $apiKey, $region); |
| 41 | + |
| 42 | + return static::createWithConfig($config); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return PersonalizationClient |
| 47 | + */ |
| 48 | + public static function createWithConfig(PersonalizationConfig $config) |
| 49 | + { |
| 50 | + $config = clone $config; |
| 51 | + |
| 52 | + if ($hosts = $config->getHosts()) { |
| 53 | + // If a list of hosts was passed, we ignore the cache |
| 54 | + $clusterHosts = ClusterHosts::create($hosts); |
| 55 | + } else { |
| 56 | + $clusterHosts = ClusterHosts::createForRecommendation($config->getRegion()); |
| 57 | + } |
| 58 | + |
| 59 | + $apiWrapper = new ApiWrapper( |
| 60 | + Algolia::getHttpClient(), |
| 61 | + $config, |
| 62 | + $clusterHosts |
| 63 | + ); |
| 64 | + |
| 65 | + return new self($apiWrapper, $config); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param array<string, int|string|array>|RequestOptions $requestOptions |
| 70 | + * |
| 71 | + * @return array<string, int|array> |
| 72 | + */ |
| 73 | + public function getPersonalizationStrategy($requestOptions = []) |
| 74 | + { |
| 75 | + return $this->api->read('GET', api_path('/1/strategies/personalization'), $requestOptions); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param array<string, int|array> $strategy |
| 80 | + * @param array<string, int|string|array>|RequestOptions $requestOptions |
| 81 | + * |
| 82 | + * @return array<string, int|string> |
| 83 | + */ |
| 84 | + public function setPersonalizationStrategy($strategy, $requestOptions = []) |
| 85 | + { |
| 86 | + return $this->api->write('POST', api_path('/1/strategies/personalization'), $strategy, $requestOptions); |
| 87 | + } |
| 88 | +} |
0 commit comments