Completed
Push — master ( ec63f5...b1dbae )
by Serhii
9s
created

SerializerSubscriber::onPrePostSerialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
crap 3.0123
1
<?php
2
3
namespace AppBundle\EventListener;
4
5
use AppBundle\Entity\History;
6
use AppBundle\Entity\Performance;
7
use AppBundle\Entity\PerformanceEvent;
8
use AppBundle\Entity\Post;
9
use AppBundle\Entity\Employee;
10
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
11
use JMS\Serializer\EventDispatcher\ObjectEvent;
12
use Sonata\MediaBundle\Controller\Api\MediaController;
13
use Symfony\Component\Routing\Router;
14
use Symfony\Component\Translation\TranslatorInterface;
15
16
class SerializerSubscriber implements EventSubscriberInterface
17
{
18
    /** @var MediaController  */
19
    protected $mediaController;
20
21
    /** @var Router */
22
    protected $router;
23
24
    /** @var  TranslatorInterface */
25
    protected $translator;
26
27 18
    public function __construct(MediaController $mediaController, Router $router, TranslatorInterface $translator)
28
    {
29 18
        $this->mediaController = $mediaController;
30 18
        $this->router = $router;
31 18
        $this->translator = $translator;
32 18
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37 1
    public static function getSubscribedEvents()
38
    {
39
        return [
40
            [
41
                'event' => 'serializer.pre_serialize',
42
                'class' => 'AppBundle\Entity\Employee',
43
                'method' => 'onPreEmployeeSerialize'
44 1
            ],
45
            [
46
                'event' => 'serializer.pre_serialize',
47
                'class' => 'AppBundle\Entity\Performance',
48
                'method' => 'onPrePerformanceSerialize'
49
            ],
50
            [
51
                'event' => 'serializer.pre_serialize',
52
                'class' => 'AppBundle\Entity\PerformanceEvent',
53
                'method' => 'onPrePerformanceEventSerialize'
54
            ],
55
            [
56
                'event' => 'serializer.pre_serialize',
57
                'class' => 'AppBundle\Entity\Post',
58
                'method' => 'onPrePostSerialize'
59
            ],
60
            [
61
                'event' => 'serializer.pre_serialize',
62
                'class' => 'AppBundle\Entity\History',
63
                'method' => 'onPreHistorySerialize'
64
            ],
65
        ];
66
    }
67
68 4
    public function onPrePerformanceEventSerialize(ObjectEvent $event)
69
    {
70
        /** @var PerformanceEvent $performanceEvent */
71 4
        $performanceEvent = $event->getObject();
72 4
        $performanceEvent->setVenue($this->translator->trans($performanceEvent->getVenue()));
73 4
    }
74
75 4 View Code Duplication
    public function onPreEmployeeSerialize(ObjectEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        /** @var Employee $employee */
78 4
        $employee = $event->getObject();
79
80 4
        if ($employee->getAvatar()) {
81 4
            $avatarLinks = $this->mediaController->getMediumFormatsAction($employee->getAvatar());
82 4
            $employee->avatarThumbnails = $avatarLinks;
83
        }
84
85 4
        $galleryHasMediaLinks = $this->formatGalleries(
86 4
            $employee->getGalleryHasMedia()->getValues(),
0 ignored issues
show
Bug introduced by
The method getValues() does not seem to exist on object<Application\Sonat...Entity\GalleryHasMedia>.

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...
87 4
            $employee->getLocale()
88
        );
89
90 4
        if ($galleryHasMediaLinks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $galleryHasMediaLinks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
91 4
            $employee->galleryHasMediaThumbnails = $galleryHasMediaLinks;
92
        }
93 4
    }
94
95 9
    public function onPrePerformanceSerialize(ObjectEvent $event)
96
    {
97
        /** @var Performance $performance */
98 9
        $performance = $event->getObject();
99
100 9
        if ($performance->getMainPicture()) {
101 9
            $mainImageLinks = $this->mediaController->getMediumFormatsAction($performance->getMainPicture());
102 9
            $performance->mainPictureThumbnails = $mainImageLinks;
103
        }
104
105 9
        if ($performance->getSliderImage()) {
106 9
            $sliderImageLinks = $this->mediaController->getMediumFormatsAction($performance->getSliderImage());
107 9
            $performance->sliderImageThumbnails = $sliderImageLinks;
108
        }
109
110 9
        $galleryHasMediaLinks = $this->formatGalleries(
111 9
            $performance->getGalleryHasMedia()->getValues(),
0 ignored issues
show
Bug introduced by
The method getValues() does not seem to exist on object<Application\Sonat...Entity\GalleryHasMedia>.

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...
112 9
            $performance->getLocale()
113
        );
114
115 9
        if ($galleryHasMediaLinks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $galleryHasMediaLinks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
116 9
            $performance->galleryHasMediaThumbnails = $galleryHasMediaLinks;
117
        }
118 9
    }
119
120 5 View Code Duplication
    public function onPrePostSerialize(ObjectEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        /** @var Post $post */
123 5
        $post = $event->getObject();
124
125 5
        if ($post->getMainPicture()) {
126 5
            $mainImageLinks = $this->mediaController->getMediumFormatsAction($post->getMainPicture());
127 5
            $post->mainPictureThumbnails = $mainImageLinks;
128
        }
129
130 5
        $galleryHasMediaLinks = $this->formatGalleries($post->getGalleryHasMedia()->getValues(), $post->getLocale());
0 ignored issues
show
Bug introduced by
The method getValues() does not seem to exist on object<Application\Sonat...Entity\GalleryHasMedia>.

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...
131
132 5
        if ($galleryHasMediaLinks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $galleryHasMediaLinks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
133
            $post->galleryHasMediaThumbnails = $galleryHasMediaLinks;
134
        }
135 5
    }
136
137 2 View Code Duplication
    public function onPreHistorySerialize(ObjectEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        /** @var History $history */
140 2
        $history = $event->getObject();
141
142 2
        if ($history->getMainPicture()) {
143 2
            $mainImageLinks = $this->mediaController->getMediumFormatsAction($history->getMainPicture());
144 2
            $history->mainPictureThumbnails = $mainImageLinks;
145
        }
146
147 2
        $galleryHasMediaLinks = $this->formatGalleries(
148 2
            $history->getGalleryHasMedia()->getValues(),
0 ignored issues
show
Bug introduced by
The method getValues() does not seem to exist on object<Application\Sonat...Entity\GalleryHasMedia>.

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...
149 2
            $history->getLocale()
150
        );
151
152 2
        if (!empty($galleryHasMediaLinks)) {
153
            $history->galleryHasMediaThumbnails = $galleryHasMediaLinks;
154
        }
155 2
    }
156
157
    /**
158
     * @param $galleries
159
     * @param $locale
160
     * @return array
161
     */
162 18
    protected function formatGalleries($galleries, $locale)
163
    {
164
        $formatedGaleries = array_filter($galleries, function ($gallery) {
165 12
            return $gallery->getMedia();
166 18
        });
167
168 18
        $formatedGaleries = array_map(function ($gallery) use ($locale) {
169
            return [
170 12
                'title' => $gallery->getTranslation('title', $locale) ?: $gallery->getTitle(),
171 12
                'decription' => $gallery->getTranslation('description', $locale) ?: $gallery->getDescription(),
172 12
                'images' => $this->mediaController->getMediumFormatsAction($gallery->getMedia()),
173
            ];
174 18
        }, $formatedGaleries);
175
176 18
        return $formatedGaleries;
177
    }
178
}
179