vendor/acseo/typesense-bundle/src/Client/TypesenseClient.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ACSEO\TypesenseBundle\Client;
  4. use Typesense\Aliases;
  5. use Typesense\Client;
  6. use Typesense\Collections;
  7. use Typesense\Debug;
  8. use Typesense\Health;
  9. use Typesense\Keys;
  10. use Typesense\Metrics;
  11. use Typesense\MultiSearch;
  12. use Typesense\Operations;
  13. class TypesenseClient
  14. {
  15.     private $client;
  16.     public function __construct(string $urlstring $apiKey)
  17.     {
  18.         if ($url === 'null') {
  19.             return;
  20.         }
  21.         $urlParsed parse_url($url);
  22.         $this->client = new Client([
  23.             'nodes' => [
  24.                 [
  25.                     'host'     => $urlParsed['host'],
  26.                     'port'     => $urlParsed['port'],
  27.                     'protocol' => $urlParsed['scheme'],
  28.                 ],
  29.             ],
  30.             'api_key'                    => $apiKey,
  31.             'connection_timeout_seconds' => 5,
  32.         ]);
  33.     }
  34.     public function getCollections(): ?Collections
  35.     {
  36.         if (!$this->client) {
  37.             return null;
  38.         }
  39.         return $this->client->collections;
  40.     }
  41.     public function getAliases(): ?Aliases
  42.     {
  43.         if (!$this->client) {
  44.             return null;
  45.         }
  46.         return $this->client->aliases;
  47.     }
  48.     public function getKeys(): ?Keys
  49.     {
  50.         if (!$this->client) {
  51.             return null;
  52.         }
  53.         return $this->client->keys;
  54.     }
  55.     public function getDebug(): ?Debug
  56.     {
  57.         if (!$this->client) {
  58.             return null;
  59.         }
  60.         return $this->client->debug;
  61.     }
  62.     public function getMetrics(): ?Metrics
  63.     {
  64.         if (!$this->client) {
  65.             return null;
  66.         }
  67.         return $this->client->metrics;
  68.     }
  69.     public function getHealth(): ?Health
  70.     {
  71.         if (!$this->client) {
  72.             return null;
  73.         }
  74.         return $this->client->health;
  75.     }
  76.     public function getOperations(): ?Operations
  77.     {
  78.         if (!$this->client) {
  79.             return null;
  80.         }
  81.         return $this->client->operations;
  82.     }
  83.     public function getMultiSearch(): ?MultiSearch
  84.     {
  85.         if (!$this->client) {
  86.             return null;
  87.         }
  88.         return $this->client->multiSearch;
  89.     }
  90.     /**
  91.      * This allow to be use to use new Typesense\Client functions
  92.      * before we update this client.
  93.      */
  94.     public function __call($name$arguments)
  95.     {
  96.         if (!$this->client) {
  97.             return null;
  98.         }
  99.         return $this->client->{$name}(...$arguments);
  100.     }
  101.     public function __get($name)
  102.     {
  103.         if (!$this->client) {
  104.             return null;
  105.         }
  106.         return $this->client->{$name};
  107.     }
  108.     public function isOperationnal(): bool
  109.     {
  110.         return $this->client !== null;
  111.     }
  112. }