SerializerSubscriber::onPrePerformanceSerialize()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.2888
c 0
b 0
f 0
cc 5
nc 9
nop 1
crap 5
1
<?php
2
3
namespace App\EventListener;
4
5
use App\Entity\History;
6
use App\Entity\Performance;
7
use App\Entity\PerformanceEvent;
8
use App\Entity\Post;
9
use App\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\Routing\RouterInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
17
class SerializerSubscriber implements EventSubscriberInterface
18
{
19
    /** @var MediaController  */
20
    protected $mediaController;
21
22
    /** @var Router */
23
    protected $router;
24
25
    /** @var  TranslatorInterface */
26
    protected $translator;
27
28 16
    public function __construct(MediaController $mediaController, RouterInterface $router, TranslatorInterface $translator)
29
    {
30 16
        $this->mediaController = $mediaController;
31 16
        $this->router = $router;
0 ignored issues
show
Documentation Bug introduced by
$router is of type object<Symfony\Component\Routing\RouterInterface>, but the property $router was declared to be of type object<Symfony\Component\Routing\Router>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
32 16
        $this->translator = $translator;
33 16
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38 1
    public static function getSubscribedEvents()
39
    {
40
        return [
41 1
            ['event' => 'serializer.pre_serialize', 'class' => 'App\Entity\Employee', 'method' => 'onPreEmployeeSerialize'],
42
            ['event' => 'serializer.pre_serialize', 'class' => 'App\Entity\Performance', 'method' => 'onPrePerformanceSerialize'],
43
            ['event' => 'serializer.pre_serialize', 'class' => 'App\Entity\PerformanceEvent', 'method' => 'onPrePerformanceEventSerialize'],
44
            ['event' => 'serializer.pre_serialize', 'class' => 'App\Entity\Post', 'method' => 'onPrePostSerialize'],
45
            ['event' => 'serializer.pre_serialize', 'class' => 'App\Entity\History', 'method' => 'onPreHistorySerialize'],
46
        ];
47
    }
48
49 3
    public function onPrePerformanceEventSerialize(ObjectEvent $event)
50
    {
51
        /** @var PerformanceEvent $performanceEvent */
52 3
        $performanceEvent = $event->getObject();
53 3
        $performanceEvent->setVenue($this->translator->trans($performanceEvent->getVenue()));
54 3
    }
55
56 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...
57
    {
58
        /** @var Employee $employee */
59 4
        $employee = $event->getObject();
60
61 4
        if ($employee->getAvatar()) {
62 4
            $avatarLinks = $this->mediaController->getMediumFormatsAction($employee->getAvatar()->getId());
63 4
            $employee->avatarThumbnails = $avatarLinks;
64
        }
65
66 4
        if ($galleryHasMediaLinks = $this->formatGalleries($employee->getGalleryHasMedia()->getValues(), $employee->getLocale())) {
0 ignored issues
show
Bug introduced by
The method getValues() does not seem to exist on object<App\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...
67 4
            $employee->galleryHasMediaThumbnails = $galleryHasMediaLinks;
68
        }
69 4
    }
70
71 8
    public function onPrePerformanceSerialize(ObjectEvent $event)
72
    {
73
        /** @var Performance $performance */
74 8
        $performance = $event->getObject();
75 8
        if (!$performance instanceof Performance) return;
76
77 8
        if ($performance->getMainPicture()) {
78 8
            $mainImageLinks = $this->mediaController->getMediumFormatsAction($performance->getMainPicture());
79 8
            $performance->mainPictureThumbnails = $mainImageLinks;
80
        }
81
82 8
        if ($performance->getSliderImage()) {
83 8
            $sliderImageLinks = $this->mediaController->getMediumFormatsAction($performance->getSliderImage());
84 8
            $performance->sliderImageThumbnails = $sliderImageLinks;
85
        }
86
87 8
        if ($galleryHasMediaLinks = $this->formatGalleries($performance->getGalleryHasMedia()->getValues(), $performance->getLocale())) {
0 ignored issues
show
Bug introduced by
The method getValues() does not seem to exist on object<App\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...
88 8
            $performance->galleryHasMediaThumbnails = $galleryHasMediaLinks;
89
        }
90 8
    }
91
92 4 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...
93
    {
94
        /** @var Post $post */
95 4
        $post = $event->getObject();
96
97 4
        if ($post->getMainPicture()) {
98 4
            $mainImageLinks = $this->mediaController->getMediumFormatsAction($post->getMainPicture());
99 4
            $post->mainPictureThumbnails = $mainImageLinks;
100
        }
101
102 4
        if ($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<App\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...
103
            $post->galleryHasMediaThumbnails = $galleryHasMediaLinks;
104
        }
105 4
    }
106
107 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...
108
    {
109
        /** @var History $history */
110 2
        $history = $event->getObject();
111
112 2
        if ($history->getMainPicture()) {
113 2
            $mainImageLinks = $this->mediaController->getMediumFormatsAction($history->getMainPicture());
114 2
            $history->mainPictureThumbnails = $mainImageLinks;
115
        }
116
117 2
        if ($galleryHasMediaLinks = $this->formatGalleries($history->getGalleryHasMedia()->getValues(), $history->getLocale())) {
0 ignored issues
show
Bug introduced by
The method getValues() does not seem to exist on object<App\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...
118
            $history->galleryHasMediaThumbnails = $galleryHasMediaLinks;
119
        }
120 2
    }
121
122 16
    protected function formatGalleries($galleries, $locale)
123
    {
124
        $formatedGaleries = array_filter($galleries, function($gallery) { return $gallery->getMedia(); });
125
        $formatedGaleries = array_map(function ($gallery) use ($locale) {
126
            return [
127 11
                'title' => $gallery->getTranslation('title', $locale) ?: $gallery->getTitle(),
128 11
                'decription' => $gallery->getTranslation('description', $locale) ?: $gallery->getDescription(),
129 11
                'images' => $this->mediaController->getMediumFormatsAction($gallery->getMedia()),
130
            ];
131 16
        }, $formatedGaleries);
132
133 16
        return $formatedGaleries;
134
    }
135
}
136