diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index dd4f232..415e0b3 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,7 +2,7 @@ "name": "multi-downloader-client", "image": "rg.fr-par.scw.cloud/kubernetes/devcontainer:php-8.3", "containerEnv": { - + "MULTI_DOWNLOADER_API_KEY": "YOUR_API_KEY" }, "customizations": { "vscode": { diff --git a/src/MultiDownloaderClient.php b/src/MultiDownloaderClient.php index ae93ef7..d4e1d20 100644 --- a/src/MultiDownloaderClient.php +++ b/src/MultiDownloaderClient.php @@ -4,4 +4,16 @@ namespace Nwb\MultiDownloaderClient; class MultiDownloaderClient { + private string $apiKey; + + public function __construct(string $apiKey = null) + { + $apiKey = $apiKey ?? getenv('MULTI_DOWNLOADER_API_KEY'); + + if (!$apiKey) { + throw new \InvalidArgumentException('API key is required'); + } + + $this->apiKey = $apiKey; + } } diff --git a/tests/ModelLibPhpTest.php b/tests/ModelLibPhpTest.php deleted file mode 100644 index 1797bac..0000000 --- a/tests/ModelLibPhpTest.php +++ /dev/null @@ -1,14 +0,0 @@ -getProperty($property); + $reflection->setAccessible(true); + + return $reflection->getValue($object); + } + + public function testApiKeyEnv() + { + putenv('MULTI_DOWNLOADER_API_KEY=1234567890'); + + $client = new MultiDownloaderClient(); + $apiKey = self::getProperty($client, 'apiKey'); + $this->assertEquals('1234567890', $apiKey); + } + + public function testApiKeyParam() + { + $client = new MultiDownloaderClient('test_key'); + + $apiKey = self::getProperty($client, 'apiKey'); + $this->assertEquals('test_key', $apiKey); + } + + public function testApiKeyMissing() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('API key is required'); + + putenv('MULTI_DOWNLOADER_API_KEY'); + + new MultiDownloaderClient(); + } +}