Completed
Push — master ( 9f23f1...4c1817 )
by Rafał
12:56
created

PackageSubscriber::onPostDeserialize()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 9
nc 7
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Bridge Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\BridgeBundle\Serializer;
16
17
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
18
use JMS\Serializer\EventDispatcher\ObjectEvent;
19
use SWP\Component\Bridge\Model\ItemInterface;
20
use SWP\Component\Bridge\Model\PackageInterface;
21
22
class PackageSubscriber implements EventSubscriberInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public static function getSubscribedEvents()
28
    {
29
        return [
30
            [
31
                'event' => 'serializer.post_deserialize',
32
                'method' => 'onPostDeserialize',
33
            ],
34
        ];
35
    }
36
37
    /**
38
     * @param ObjectEvent $event
39
     */
40
    public function onPostDeserialize(ObjectEvent $event)
41
    {
42
        if ($event->getObject() instanceof PackageInterface) {
43
            /** @var PackageInterface $package */
44
            $package = $event->getObject();
45
46
            foreach ($package->getItems() as $item) {
47
                $this->processRenditions($item);
48
49
                foreach ($item->getItems() as $imageItem) {
50
                    $this->processRenditions($imageItem);
51
                }
52
            }
53
54
            foreach ($package->getItems() as $key => $item) {
55
                $item->setName($key);
56
            }
57
        }
58
    }
59
60
    private function processRenditions(ItemInterface $item)
61
    {
62
        foreach ($item->getRenditions() as $key => $rendition) {
63
            $rendition->setName($key);
64
            $rendition->setItem($item);
65
        }
66
    }
67
}
68