49 lines
1.4 KiB
PHP
49 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Tests\Domain\Scraping\Adapter;
|
|
|
|
use App\Domain\Scraping\Domain\Contract\Service\ImageDownloaderInterface;
|
|
use App\Domain\Scraping\Domain\Model\ValueObject\DownloadResult;
|
|
use App\Domain\Scraping\Domain\Model\ValueObject\TempDirectory;
|
|
|
|
class InMemoryImageDownloader implements ImageDownloaderInterface
|
|
{
|
|
private array $downloadedFiles = [];
|
|
private ?\Exception $shouldThrowException = null;
|
|
|
|
public function download(string $url, string $destination): void
|
|
{
|
|
if ($this->shouldThrowException) {
|
|
throw $this->shouldThrowException;
|
|
}
|
|
|
|
$this->downloadedFiles[$url] = $destination;
|
|
}
|
|
|
|
public function downloadBatch(array $urls, TempDirectory $tempDir, string $jobId): array
|
|
{
|
|
if ($this->shouldThrowException) {
|
|
throw $this->shouldThrowException;
|
|
}
|
|
|
|
$results = [];
|
|
foreach ($urls as $index => $url) {
|
|
$destination = sprintf('%s/%03d.jpg', $tempDir->getPath(), $index + 1);
|
|
$this->download($url, $destination);
|
|
$results[] = new DownloadResult($destination, $url);
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
public function simulateError(\Exception $exception): void
|
|
{
|
|
$this->shouldThrowException = $exception;
|
|
}
|
|
|
|
public function getDownloadedFiles(): array
|
|
{
|
|
return $this->downloadedFiles;
|
|
}
|
|
}
|