Completed
Push — master ( a77988...c50359 )
by Rafał
02:00 queued 11s
created

PackageSubscriber::onPreSerialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
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\Bundle\ContentBundle\Model\MediaAwareInterface;
20
use SWP\Component\Bridge\Model\ItemInterface;
21
use SWP\Component\Bridge\Model\Package;
22
use SWP\Component\Bridge\Model\PackageInterface;
23
24
class PackageSubscriber implements EventSubscriberInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public static function getSubscribedEvents()
30
    {
31
        return [
32
            [
33
                'event' => 'serializer.post_deserialize',
34
                'method' => 'onPostDeserialize',
35
            ],
36
            [
37
                'event' => 'serializer.pre_serialize',
38
                'method' => 'onPreSerialize',
39
            ],
40
        ];
41
    }
42
43
    public function onPostDeserialize(ObjectEvent $event)
44
    {
45
        /** @var PackageInterface $package */
46
        $package = $event->getObject();
47
        if ($package instanceof PackageInterface) {
48
            foreach ($package->getItems() as $item) {
49
                $this->processRenditions($item);
50
51
                foreach ($item->getItems() as $imageItem) {
52
                    $this->processRenditions($imageItem);
53
                }
54
            }
55
56
            $this->processGroups($package);
57
58
            foreach ($package->getItems() as $key => $item) {
59
                $item->setName($key);
60
            }
61
62
            $this->setFeatureMedia($package);
63
        }
64
    }
65
66
    public function onPreSerialize(ObjectEvent $event)
67
    {
68
        /** @var PackageInterface $package */
69
        $package = $event->getObject();
70
71
        if ($package instanceof PackageInterface) {
72
            $this->setFeatureMedia($package);
73
        }
74
    }
75
76
    private function processGroups(PackageInterface $package): void
77
    {
78
        foreach ((array) $package->getGroups() as $groups) {
79
            foreach ((array) $groups as $key => $group) {
80
                $group->setCode($key);
81
                foreach ($group->getItems() as $groupItem) {
82
                    $groupItem->setGroup($group);
83
                    $groupItem->setName($groupItem->getGuid());
84
                    $this->processRenditions($groupItem);
85
                }
86
87
                $group->setPackage($package);
88
            }
89
        }
90
    }
91
92
    private function setFeatureMedia(PackageInterface $package)
93
    {
94
        /** @var ItemInterface $item */
95
        foreach ($package->getItems() as $item) {
96
            if (MediaAwareInterface::KEY_FEATURE_MEDIA === $item->getName()) {
97
                $package->setFeatureMedia($item);
0 ignored issues
show
Bug introduced by
The method setFeatureMedia() does not seem to exist on object<SWP\Component\Bri...Model\PackageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
            }
99
        }
100
    }
101
102
    private function processRenditions(ItemInterface $item)
103
    {
104
        foreach ($item->getRenditions() as $key => $rendition) {
105
            $rendition->setName($key);
106
            $rendition->setItem($item);
107
        }
108
    }
109
}
110