vendor/typesense/typesense-php/src/Lib/Configuration.php line 222

Open in your IDE?
  1. <?php
  2. namespace Typesense\Lib;
  3. use Http\Client\Common\HttpMethodsClient;
  4. use Http\Client\HttpClient;
  5. use Http\Discovery\HttpClientDiscovery;
  6. use Http\Discovery\MessageFactoryDiscovery;
  7. use Monolog\Handler\StreamHandler;
  8. use Monolog\Logger;
  9. use Psr\Log\LoggerInterface;
  10. use Typesense\Exceptions\ConfigError;
  11. /**
  12.  * Class Configuration
  13.  *
  14.  * @package \Typesense
  15.  * @date    4/5/20
  16.  * @author  Abdullah Al-Faqeir <abdullah@devloops.net>
  17.  */
  18. class Configuration
  19. {
  20.     /**
  21.      * @var Node[]
  22.      */
  23.     private array $nodes;
  24.     /**
  25.      * @var Node|null
  26.      */
  27.     private ?Node $nearestNode;
  28.     /**
  29.      * @var string
  30.      */
  31.     private string $apiKey;
  32.     /**
  33.      * @var float
  34.      */
  35.     private float $numRetries;
  36.     /**
  37.      * @var float
  38.      */
  39.     private float $retryIntervalSeconds;
  40.     /**
  41.      * @var int
  42.      */
  43.     private int $healthCheckIntervalSeconds;
  44.     /**
  45.      * @var LoggerInterface
  46.      */
  47.     private LoggerInterface $logger;
  48.     /**
  49.      * @var null|HttpClient
  50.      */
  51.     private ?HttpClient $client null;
  52.     /**
  53.      * @var int
  54.      */
  55.     private int $logLevel;
  56.     /**
  57.      * Configuration constructor.
  58.      *
  59.      * @param array $config
  60.      *
  61.      * @throws ConfigError
  62.      */
  63.     public function __construct(array $config)
  64.     {
  65.         $this->validateConfigArray($config);
  66.         $nodes $config['nodes'] ?? [];
  67.         foreach ($nodes as $node) {
  68.             $this->nodes[] = new Node($node['host'], $node['port'], $node['path'] ?? ''$node['protocol']);
  69.         }
  70.         $nearestNode       $config['nearest_node'] ?? null;
  71.         $this->nearestNode null;
  72.         if (null !== $nearestNode) {
  73.             $this->nearestNode =
  74.                 new Node(
  75.                     $nearestNode['host'],
  76.                     $nearestNode['port'],
  77.                     $nearestNode['path'] ?? '',
  78.                     $nearestNode['protocol']
  79.                 );
  80.         }
  81.         $this->apiKey $config['api_key'] ?? '';
  82.         $this->healthCheckIntervalSeconds = (int)($config['healthcheck_interval_seconds'] ?? 60);
  83.         $this->numRetries           = (float)($config['num_retries'] ?? 3);
  84.         $this->retryIntervalSeconds = (float)($config['retry_interval_seconds'] ?? 1.0);
  85.         $this->logLevel $config['log_level'] ?? Logger::WARNING;
  86.         $this->logger   = new Logger('typesense');
  87.         $this->logger->pushHandler(new StreamHandler('php://stdout'$this->logLevel));
  88.         if (true === \array_key_exists('client'$config) && $config['client'] instanceof HttpClient) {
  89.             $this->client $config['client'];
  90.         }
  91.     }
  92.     /**
  93.      * @param array $config
  94.      *
  95.      * @throws ConfigError
  96.      */
  97.     private function validateConfigArray(array $config): void
  98.     {
  99.         $nodes $config['nodes'] ?? false;
  100.         if (!$nodes) {
  101.             throw new ConfigError('`nodes` is not defined.');
  102.         }
  103.         $apiKey $config['api_key'] ?? false;
  104.         if (!$apiKey) {
  105.             throw new ConfigError('`api_key` is not defined.');
  106.         }
  107.         foreach ($nodes as $node) {
  108.             if (!$this->validateNodeFields($node)) {
  109.                 throw new ConfigError(
  110.                     '`node` entry be a dictionary with the following required keys: host, port, protocol, api_key'
  111.                 );
  112.             }
  113.         }
  114.         $nearestNode $config['nearest_node'] ?? [];
  115.         if (!empty($nearestNode) && !$this->validateNodeFields($nearestNode)) {
  116.             throw new ConfigError(
  117.                 '`nearest_node` entry be a dictionary with the following required keys: host, port, protocol, api_key'
  118.             );
  119.         }
  120.     }
  121.     /**
  122.      * @param array $node
  123.      *
  124.      * @return bool
  125.      */
  126.     public function validateNodeFields(array $node): bool
  127.     {
  128.         $keys = [
  129.             'host',
  130.             'port',
  131.             'protocol',
  132.         ];
  133.         return !array_diff_key(array_flip($keys), $node);
  134.     }
  135.     /**
  136.      * @return Node[]
  137.      */
  138.     public function getNodes(): array
  139.     {
  140.         return $this->nodes;
  141.     }
  142.     /**
  143.      * @return Node
  144.      */
  145.     public function getNearestNode(): ?Node
  146.     {
  147.         return $this->nearestNode;
  148.     }
  149.     /**
  150.      * @return mixed|string
  151.      */
  152.     public function getApiKey()
  153.     {
  154.         return $this->apiKey;
  155.     }
  156.     /**
  157.      * @return float
  158.      */
  159.     public function getNumRetries(): float
  160.     {
  161.         return $this->numRetries;
  162.     }
  163.     /**
  164.      * @return float
  165.      */
  166.     public function getRetryIntervalSeconds(): float
  167.     {
  168.         return $this->retryIntervalSeconds;
  169.     }
  170.     /**
  171.      * @return float|mixed
  172.      */
  173.     public function getHealthCheckIntervalSeconds()
  174.     {
  175.         return $this->healthCheckIntervalSeconds;
  176.     }
  177.     /**
  178.      * @return LoggerInterface
  179.      */
  180.     public function getLogger(): LoggerInterface
  181.     {
  182.         return $this->logger;
  183.     }
  184.     /**
  185.      * @return HttpClient
  186.      */
  187.     public function getClient(): HttpClient
  188.     {
  189.         return new HttpMethodsClient(
  190.             $this->client ?? HttpClientDiscovery::find(),
  191.             MessageFactoryDiscovery::find()
  192.         );
  193.     }
  194. }