Handle 404 for assigned media

This commit is contained in:
Daniel Siepmann 2021-06-08 11:54:20 +02:00
parent baf32b3477
commit 9c60e275d0
2 changed files with 70 additions and 6 deletions

View file

@ -22,6 +22,7 @@ namespace WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser;
*/
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData;
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData\InvalidResponseException;
class Media
{
@ -40,36 +41,73 @@ class Media
{
$media = [];
if (isset($jsonLD['schema:photo']['@id'])) {
$media = $this->addMainImage($jsonLD, $media);
$media = $this->addSingleImage($jsonLD, $media);
$media = $this->addImages($jsonLD, $media);
return $media;
}
private function addMainImage(array $jsonLD, array $media): array
{
if (isset($jsonLD['schema:photo']['@id']) === false) {
return $media;
}
try {
$media[] = array_merge(
[
'mainImage' => true,
],
$this->getMedia($jsonLD['schema:photo']['@id'])
);
} catch (InvalidResponseException $e) {
// Nothing todo
}
if (isset($jsonLD['schema:image']['@id'])) {
return $media;
}
private function addSingleImage(array $jsonLD, array $media): array
{
if (isset($jsonLD['schema:image']['@id']) === false) {
return $media;
}
try {
$media[] = array_merge(
[
'mainImage' => false,
],
$this->getMedia($jsonLD['schema:image']['@id'])
);
} catch (InvalidResponseException $e) {
// Nothing todo
}
return $media;
}
private function addImages(array $jsonLD, array $media): array
{
if (
isset($jsonLD['schema:image'])
&& isset($jsonLD['schema:image']['@id']) === false
&& is_array($jsonLD['schema:image'])
isset($jsonLD['schema:image']) === false
|| isset($jsonLD['schema:image']['@id'])
|| is_array($jsonLD['schema:image']) === false
) {
foreach ($jsonLD['schema:image'] as $image) {
return $media;
}
foreach ($jsonLD['schema:image'] as $image) {
try {
$media[] = array_merge(
[
'mainImage' => false,
],
$this->getMedia($image['@id'])
);
} catch (InvalidResponseException $e) {
// Nothing todo
}
}

View file

@ -24,6 +24,7 @@ namespace WerkraumMedia\ThueCat\Tests\Unit\Domain\Import\JsonLD\Parser;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData;
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData\InvalidResponseException;
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\Media;
/**
@ -142,6 +143,31 @@ class MediaTest extends TestCase
], $result);
}
/**
* @test
*/
public function returnsEmptyArrayOn404(): void
{
$fetchData = $this->prophesize(FetchData::class);
$fetchData->jsonLDFromUrl('https://thuecat.org/resources/dms_5099196')->willThrow(new InvalidResponseException());
$subject = new Media(
$fetchData->reveal()
);
$result = $subject->get([
'schema:photo' => [
'@id' => 'https://thuecat.org/resources/dms_5099196',
],
'schema:image' => [
'@id' => 'https://thuecat.org/resources/dms_5099196',
],
]);
self::assertSame([], $result);
}
/**
* @test
*/