diff --git a/README.md b/README.md index 85bdb74..cf152a3 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,6 @@ $form = $client->htmlForm(); ``` Rename files inside zip ```php -$file = (new FileRequest('http://example.com/image.png')) - ->fileOptions([ - 'name' => 'my-new-name.png' - ]); +$file = new FileRequest('http://example.com/image.png'); +$file->getFileOptions()->name('new-name.png'); ``` diff --git a/src/FileOptions.php b/src/FileOptions.php new file mode 100644 index 0000000..18659bf --- /dev/null +++ b/src/FileOptions.php @@ -0,0 +1,30 @@ +name = $name; + + return $this; + } + + public function getName(): string + { + return $this->name; + } + + public function toArray(): array + { + return [ + 'name' => $this->name + ]; + } +} diff --git a/src/FileRequest.php b/src/FileRequest.php index 36c4e1b..d25e494 100644 --- a/src/FileRequest.php +++ b/src/FileRequest.php @@ -4,14 +4,25 @@ namespace Nwb\MultiDownloaderClient; class FileRequest { + /** + * @var string + */ private $url; + /** + * @var string + */ private $fallbackUrl; - private $fileOptions = []; + /** + * @var FileOptions + */ + private $fileOptions; public function __construct(string $url, string $fallbackUrl = null) { $this->url = $url; $this->fallbackUrl = $fallbackUrl; + + $this->fileOptions = new FileOptions(); } public function url(string $url) @@ -28,14 +39,14 @@ class FileRequest return $this; } - public function fileOptions(array $fileOptions) + public function fileOptions(FileOptions $fileOptions) { $this->fileOptions = $fileOptions; return $this; } - public function getFileOptions(): array + public function getFileOptions(): FileOptions { return $this->fileOptions; } @@ -55,7 +66,7 @@ class FileRequest return array_filter([ 'url' => $this->url, 'fallbackUrl' => $this->fallbackUrl, - 'fileOptions' => $this->fileOptions, + 'fileOptions' => $this->fileOptions->toArray(), ]); } } diff --git a/src/MultiDownloaderClient.php b/src/MultiDownloaderClient.php index ec53b6d..9bc677d 100644 --- a/src/MultiDownloaderClient.php +++ b/src/MultiDownloaderClient.php @@ -23,15 +23,19 @@ class MultiDownloaderClient public function __construct(array $options = []) { $apiKey = $options['apiKey'] ?? getenv('MULTI_DOWNLOADER_ACCESS_KEY'); + if (!$apiKey) { throw new \InvalidArgumentException('API key is required'); } + $this->apiKey = $apiKey; $apiSecret = $options['apiSecret'] ?? getenv('MULTI_DOWNLOADER_SECRET_KEY'); + if (!$apiSecret) { throw new \InvalidArgumentException('API secret key is required'); } + $this->apiSecret = $apiSecret; $this->url = $options['url'] ?? getenv('MULTI_DOWNLOADER_URL') ?: 'https://multi-dl.kub.nwb.fr'; diff --git a/tests/MultiDownloaderClientTest.php b/tests/MultiDownloaderClientTest.php index d5867c3..d975776 100644 --- a/tests/MultiDownloaderClientTest.php +++ b/tests/MultiDownloaderClientTest.php @@ -16,8 +16,10 @@ class MultiDownloaderClientTest extends TestCase private function testFiles() { return array_map(function ($url, $i) { - return (new FileRequest($url)) - ->fileOptions(['name' => 'test' . $i . '.png']); + $file = new FileRequest($url); + $file->getFileOptions()->name('test' . $i . '.png'); + + return $file; }, $this->testFiles, array_keys($this->testFiles)); } @@ -38,7 +40,7 @@ class MultiDownloaderClientTest extends TestCase { $client = new MultiDownloaderClient(['apiKey' => 'test', 'apiSecret' => 'test']); - $path = __DIR__ . '/testDownloadTo.zip'; + $path = sys_get_temp_dir() . '/testDownloadTo.zip'; $client->setFiles($this->testFiles()); $client->downloadTo($path); diff --git a/tests/testDownloadTo.zip b/tests/testDownloadTo.zip deleted file mode 100644 index 8a48d8d..0000000 Binary files a/tests/testDownloadTo.zip and /dev/null differ