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) |
|
|
|
|
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(), |
|
|
|
|
87
|
4 |
|
$employee->getLocale() |
88
|
|
|
); |
89
|
|
|
|
90
|
4 |
|
if ($galleryHasMediaLinks) { |
|
|
|
|
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(), |
|
|
|
|
112
|
9 |
|
$performance->getLocale() |
113
|
|
|
); |
114
|
|
|
|
115
|
9 |
|
if ($galleryHasMediaLinks) { |
|
|
|
|
116
|
9 |
|
$performance->galleryHasMediaThumbnails = $galleryHasMediaLinks; |
117
|
|
|
} |
118
|
9 |
|
} |
119
|
|
|
|
120
|
5 |
View Code Duplication |
public function onPrePostSerialize(ObjectEvent $event) |
|
|
|
|
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()); |
|
|
|
|
131
|
|
|
|
132
|
5 |
|
if ($galleryHasMediaLinks) { |
|
|
|
|
133
|
|
|
$post->galleryHasMediaThumbnails = $galleryHasMediaLinks; |
134
|
|
|
} |
135
|
5 |
|
} |
136
|
|
|
|
137
|
2 |
View Code Duplication |
public function onPreHistorySerialize(ObjectEvent $event) |
|
|
|
|
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(), |
|
|
|
|
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
|
|
|
|
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.