Completed
Push — master ( 56720f...a77988 )
by Rafał
16:32 queued 11s
created

PackageSubscriber::processGroups()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
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
            $this->processGroups($package);
55
56
            foreach ($package->getItems() as $key => $item) {
57
                $item->setName($key);
58
            }
59
        }
60
    }
61
62
    private function processGroups(PackageInterface $package): void
63
    {
64
        foreach ((array) $package->getGroups() as $groups) {
65
            foreach ((array) $groups as $key => $group) {
66
                $group->setCode($key);
67
                foreach ($group->getItems() as $groupItem) {
68
                    $groupItem->setGroup($group);
69
                    $groupItem->setName($groupItem->getGuid());
70
                    $this->processRenditions($groupItem);
71
                }
72
73
                $group->setPackage($package);
74
            }
75
        }
76
    }
77
78
    private function processRenditions(ItemInterface $item)
79
    {
80
        foreach ($item->getRenditions() as $key => $rendition) {
81
            $rendition->setName($key);
82
            $rendition->setItem($item);
83
        }
84
    }
85
}
86