mirror of
https://github.com/werkraum-media/thuecat.git
synced 2024-07-01 07:48:53 +02:00
Handle 404 for assigned media
This commit is contained in:
parent
baf32b3477
commit
9c60e275d0
|
@ -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;
|
||||||
|
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData\InvalidResponseException;
|
||||||
|
|
||||||
class Media
|
class Media
|
||||||
{
|
{
|
||||||
|
@ -40,36 +41,73 @@ class Media
|
||||||
{
|
{
|
||||||
$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(
|
$media[] = array_merge(
|
||||||
[
|
[
|
||||||
'mainImage' => true,
|
'mainImage' => true,
|
||||||
],
|
],
|
||||||
$this->getMedia($jsonLD['schema:photo']['@id'])
|
$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(
|
$media[] = array_merge(
|
||||||
[
|
[
|
||||||
'mainImage' => false,
|
'mainImage' => false,
|
||||||
],
|
],
|
||||||
$this->getMedia($jsonLD['schema:image']['@id'])
|
$this->getMedia($jsonLD['schema:image']['@id'])
|
||||||
);
|
);
|
||||||
|
} catch (InvalidResponseException $e) {
|
||||||
|
// Nothing todo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $media;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addImages(array $jsonLD, array $media): array
|
||||||
|
{
|
||||||
if (
|
if (
|
||||||
isset($jsonLD['schema:image'])
|
isset($jsonLD['schema:image']) === false
|
||||||
&& isset($jsonLD['schema:image']['@id']) === false
|
|| isset($jsonLD['schema:image']['@id'])
|
||||||
&& is_array($jsonLD['schema:image'])
|
|| is_array($jsonLD['schema:image']) === false
|
||||||
) {
|
) {
|
||||||
|
return $media;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($jsonLD['schema:image'] as $image) {
|
foreach ($jsonLD['schema:image'] as $image) {
|
||||||
|
try {
|
||||||
$media[] = array_merge(
|
$media[] = array_merge(
|
||||||
[
|
[
|
||||||
'mainImage' => false,
|
'mainImage' => false,
|
||||||
],
|
],
|
||||||
$this->getMedia($image['@id'])
|
$this->getMedia($image['@id'])
|
||||||
);
|
);
|
||||||
|
} catch (InvalidResponseException $e) {
|
||||||
|
// Nothing todo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ namespace WerkraumMedia\ThueCat\Tests\Unit\Domain\Import\JsonLD\Parser;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
use Prophecy\PhpUnit\ProphecyTrait;
|
||||||
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData;
|
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData;
|
||||||
|
use WerkraumMedia\ThueCat\Domain\Import\Importer\FetchData\InvalidResponseException;
|
||||||
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\Media;
|
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\Media;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -142,6 +143,31 @@ class MediaTest extends TestCase
|
||||||
], $result);
|
], $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
|
* @test
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue