1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2019 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2019 Sourcefabric z.ú. |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Serializer; |
18
|
|
|
|
19
|
|
|
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
21
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
22
|
|
|
use JMS\Serializer\Metadata\StaticPropertyMetadata; |
23
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
24
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRendition; |
25
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; |
26
|
|
|
|
27
|
|
|
final class ImageRenditionSerializationSubscriber implements EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
private $mediaManager; |
30
|
|
|
|
31
|
|
|
public function __construct(MediaManagerInterface $mediaManager) |
32
|
|
|
{ |
33
|
|
|
$this->mediaManager = $mediaManager; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getSubscribedEvents(): array |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
[ |
40
|
|
|
'event' => 'serializer.post_serialize', |
41
|
|
|
'class' => ImageRendition::class, |
42
|
|
|
'method' => 'onPostSerialize', |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function onPostSerialize(ObjectEvent $event): void |
48
|
|
|
{ |
49
|
|
|
/** @var JsonSerializationVisitor $visitor */ |
50
|
|
|
$visitor = $event->getVisitor(); |
51
|
|
|
/** @var ImageRenditionInterface $rendition */ |
52
|
|
|
$rendition = $event->getObject(); |
53
|
|
|
|
54
|
|
|
$visitor->visitProperty( |
55
|
|
|
new StaticPropertyMetadata('', '_links', null), |
56
|
|
|
[ |
57
|
|
|
'public_url' => [ |
58
|
|
|
'href' => $this->mediaManager->getMediaPublicUrl($rendition->getImage()), |
59
|
|
|
], |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|