Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| Configuration | |
100.00% |
28 / 28 |
|
100.00% |
7 / 7 |
14 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |||
| getApiKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getControllerHost | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAdditionalHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTimeout | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDefaultHeaders | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getApiKeyFromEnvironment | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Mbvb1223\Pinecone\Utils; |
| 6 | |
| 7 | use Mbvb1223\Pinecone\Errors\PineconeValidationException; |
| 8 | |
| 9 | class Configuration |
| 10 | { |
| 11 | private readonly string $apiKey; |
| 12 | private readonly string $controllerHost; |
| 13 | /** @var array<string, string> */ |
| 14 | private readonly array $additionalHeaders; |
| 15 | private readonly int $timeout; |
| 16 | |
| 17 | /** @param array{controllerHost?: string, additionalHeaders?: array<string, string>, timeout?: int}|null $config */ |
| 18 | public function __construct(?string $apiKey = null, ?array $config = null) |
| 19 | { |
| 20 | $this->apiKey = $apiKey ?? $this->getApiKeyFromEnvironment(); |
| 21 | $controllerHost = $config['controllerHost'] ?? 'https://api.pinecone.io'; |
| 22 | $this->controllerHost = rtrim($controllerHost, '/'); |
| 23 | $this->additionalHeaders = $config['additionalHeaders'] ?? []; |
| 24 | |
| 25 | $timeout = isset($config['timeout']) ? (int) $config['timeout'] : 30; |
| 26 | if ($timeout <= 0) { |
| 27 | throw new PineconeValidationException('Timeout must be a positive integer.'); |
| 28 | } |
| 29 | $this->timeout = $timeout; |
| 30 | |
| 31 | if (empty($this->apiKey)) { |
| 32 | throw new PineconeValidationException('API key is required. Set PINECONE_API_KEY environment variable or pass it in configuration.'); |
| 33 | } |
| 34 | |
| 35 | $scheme = parse_url($this->controllerHost, PHP_URL_SCHEME); |
| 36 | if ($scheme !== 'https' && $scheme !== 'http') { |
| 37 | throw new PineconeValidationException('Controller host must be a valid URL with http or https scheme.'); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public function getApiKey(): string |
| 42 | { |
| 43 | return $this->apiKey; |
| 44 | } |
| 45 | |
| 46 | public function getControllerHost(): string |
| 47 | { |
| 48 | return $this->controllerHost; |
| 49 | } |
| 50 | |
| 51 | /** @return array<string, string> */ |
| 52 | public function getAdditionalHeaders(): array |
| 53 | { |
| 54 | return $this->additionalHeaders; |
| 55 | } |
| 56 | |
| 57 | public function getTimeout(): int |
| 58 | { |
| 59 | return $this->timeout; |
| 60 | } |
| 61 | |
| 62 | /** @return array<string, string> */ |
| 63 | public function getDefaultHeaders(): array |
| 64 | { |
| 65 | return array_merge([ |
| 66 | 'Api-Key' => $this->apiKey, |
| 67 | 'User-Agent' => 'pinecone-php-client/1.0.0', |
| 68 | 'Content-Type' => 'application/json', |
| 69 | 'X-Pinecone-Api-Version' => '2025-10', |
| 70 | ], $this->additionalHeaders); |
| 71 | } |
| 72 | |
| 73 | private function getApiKeyFromEnvironment(): string |
| 74 | { |
| 75 | $envKey = $_ENV['PINECONE_API_KEY'] ?? null; |
| 76 | if ($envKey !== null) { |
| 77 | return $envKey; |
| 78 | } |
| 79 | |
| 80 | $getenvKey = getenv('PINECONE_API_KEY'); |
| 81 | |
| 82 | return $getenvKey !== false ? $getenvKey : ''; |
| 83 | } |
| 84 | } |