Completed
Push — 2.0 ( 359ae1...221cc7 )
by Rafał
51:27 queued 18:07
created

ImageRenditionSerializationSubscriber   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 10 1
A onPostSerialize() 0 16 1
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