FileOptions class
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mathieu 2024-01-12 14:00:06 +00:00
parent bd500406d3
commit e389e72844
6 changed files with 56 additions and 11 deletions

View File

@ -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');
```

30
src/FileOptions.php Normal file
View File

@ -0,0 +1,30 @@
<?php
namespace Nwb\MultiDownloaderClient;
class FileOptions
{
/**
* @var string
*/
private $name;
public function name(string $name)
{
$this->name = $name;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function toArray(): array
{
return [
'name' => $this->name
];
}
}

View File

@ -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(),
]);
}
}

View File

@ -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';

View File

@ -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);

Binary file not shown.