|
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; |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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())) { |
|
|
|
|
|
|
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())) { |
|
|
|
|
|
|
88
|
8 |
|
$performance->galleryHasMediaThumbnails = $galleryHasMediaLinks; |
|
89
|
|
|
} |
|
90
|
8 |
|
} |
|
91
|
|
|
|
|
92
|
4 |
View Code Duplication |
public function onPrePostSerialize(ObjectEvent $event) |
|
|
|
|
|
|
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())) { |
|
|
|
|
|
|
103
|
|
|
$post->galleryHasMediaThumbnails = $galleryHasMediaLinks; |
|
104
|
|
|
} |
|
105
|
4 |
|
} |
|
106
|
|
|
|
|
107
|
2 |
View Code Duplication |
public function onPreHistorySerialize(ObjectEvent $event) |
|
|
|
|
|
|
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())) { |
|
|
|
|
|
|
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
|
|
|
|
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.