-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMediaEventSubscriber.php
205 lines (171 loc) · 5.14 KB
/
MediaEventSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
declare(strict_types=1);
/*
* This file is part of the ekino/tiny-png-sonata-media-bundle project.
*
* (c) Ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ekino\TinyPngSonataMediaBundle\Listener;
use Doctrine\Common\EventArgs;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
use Ekino\TinyPngSonataMediaBundle\Consumer\OptimizeImageConsumer;
use Sonata\MediaBundle\Filesystem\Local;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Provider\Pool;
use Sonata\NotificationBundle\Backend\BackendInterface;
use Symfony\Component\HttpFoundation\File\File;
/**
* Class MediaEventSubscriber
*
* @author Benoit Mazière <benoit.maziere@ekino.com>
*/
final class MediaEventSubscriber implements EventSubscriber
{
private const ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg'];
/**
* @var array
*/
private $providers;
/**
* @var Pool
*/
private $pool;
/**
* @var BackendInterface
*/
private $backend;
/**
* @param array $providers
* @param Pool $pool
* @param BackendInterface $backend
*/
public function __construct(array $providers, Pool $pool, BackendInterface $backend)
{
$this->providers = $providers;
$this->pool = $pool;
$this->backend = $backend;
}
/**
* {@inheritdoc}
*/
public function getSubscribedEvents(): array
{
return [
Events::postUpdate,
Events::postPersist,
];
}
/**
* @param EventArgs $args
*/
public function postUpdate(EventArgs $args): void
{
if (!($provider = $this->getProvider($args))) {
return;
}
if (!($media = $this->getMedia($args))) {
return;
}
if (!$this->doesBinaryContentChanged($args, $media)) {
return;
}
$this->publishOptimizeMessage($media);
}
/**
* @param EventArgs $args
*/
public function postPersist(EventArgs $args): void
{
if (!($provider = $this->getProvider($args))) {
return;
}
if (!($media = $this->getMedia($args))) {
return;
}
$this->publishOptimizeMessage($media);
}
/**
* @param EventArgs $args
*
* @return MediaProviderInterface|null
*/
private function getProvider(EventArgs $args): ?MediaProviderInterface
{
$media = $this->getMedia($args);
if (!$media instanceof MediaInterface) {
return null;
}
if (!\in_array($media->getProviderName(), $this->providers)) {
return null;
}
if (!\in_array($media->getExtension(), static::ALLOWED_EXTENSIONS)) {
return null;
}
$provider = $this->pool->getProvider($media->getProviderName());
if (!$provider->getFilesystem()->getAdapter() instanceof Local) {
return null;
}
return $provider;
}
/**
* @param EventArgs $args
*
* @return MediaInterface|null
*/
private function getMedia(EventArgs $args): ?MediaInterface
{
if (!$args instanceof LifecycleEventArgs) {
return null;
}
$media = $args->getEntity();
if (!$media instanceof MediaInterface) {
return null;
}
return $media;
}
/**
* @param MediaInterface $media
*/
private function publishOptimizeMessage(MediaInterface $media): void
{
$this->backend->createAndPublish(OptimizeImageConsumer::CONSUMER_TYPE, [
'mediaId' => $media->getId(),
]);
}
/**
* This method intends to detect if binaryContent changed to prevent relaunched of optimization if binaryContent did not change.
* This can happen if the binary is not changed (only metadata) or after update size in OptimizeImageConsumer.
* It can be improved as for now it is only based on the fact that the size of the media changed.
*
* @param EventArgs $args
* @param MediaInterface $media
*
* @return bool
*/
private function doesBinaryContentChanged(EventArgs $args, MediaInterface $media): bool
{
if (!$args instanceof LifecycleEventArgs) {
return false;
}
/** @var EntityManager $em */
$em = $args->getEntityManager();
$provider = $this->pool->getProvider($media->getProviderName());
/** @var Local $adapter */
$adapter = $provider->getFilesystem()->getAdapter();
$directory = $adapter->getDirectory();
$path = sprintf('%s/%s',
$directory, $provider->generatePrivateUrl($media, MediaProviderInterface::FORMAT_REFERENCE)
);
$file = new File($path);
$size = $file->getSize();
return \array_key_exists('size', $em->getUnitOfWork()->getEntityChangeSet($media))
&& $size !== $em->getUnitOfWork()->getEntityChangeSet($media)['size'][1];
}
}