Completed
Push — master ( 2bbc8c...a19f79 )
by
unknown
11:33
created

BaseProvider::setResizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Provider;
15
16
use Gaufrette\Filesystem;
17
use Sonata\CoreBundle\Validator\ErrorElement;
18
use Sonata\MediaBundle\CDN\CDNInterface;
19
use Sonata\MediaBundle\Generator\GeneratorInterface;
20
use Sonata\MediaBundle\Model\MediaInterface;
21
use Sonata\MediaBundle\Resizer\ResizerInterface;
22
use Sonata\MediaBundle\Thumbnail\ThumbnailInterface;
23
24
abstract class BaseProvider implements MediaProviderInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $formats = [];
30
31
    /**
32
     * @var string[]
33
     */
34
    protected $templates = [];
35
36
    /**
37
     * @var ResizerInterface
38
     */
39
    protected $resizer;
40
41
    /**
42
     * @var Filesystem
43
     */
44
    protected $filesystem;
45
46
    /**
47
     * @var GeneratorInterface
48
     */
49
    protected $pathGenerator;
50
51
    /**
52
     * @var CDNInterface
53
     */
54
    protected $cdn;
55
56
    /**
57
     * @var ThumbnailInterface
58
     */
59
    protected $thumbnail;
60
61
    /**
62
     * @var string
63
     */
64
    protected $name;
65
66
    /**
67
     * @var MediaInterface[]
68
     */
69
    private $clones = [];
70
71
    /**
72
     * @param string $name
73
     */
74
    public function __construct($name, Filesystem $filesystem, CDNInterface $cdn, GeneratorInterface $pathGenerator, ThumbnailInterface $thumbnail)
75
    {
76
        $this->name = $name;
77
        $this->filesystem = $filesystem;
78
        $this->cdn = $cdn;
79
        $this->pathGenerator = $pathGenerator;
80
        $this->thumbnail = $thumbnail;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    final public function transform(MediaInterface $media): void
87
    {
88
        if (null === $media->getBinaryContent()) {
89
            return;
90
        }
91
92
        $this->doTransform($media);
93
        $this->flushCdn($media);
94
    }
95
96
    public function flushCdn(MediaInterface $media): void
97
    {
98
        if ($media->getId() && $this->requireThumbnails() && !$media->getCdnIsFlushable()) {
99
            $flushPaths = [];
100
            foreach ($this->getFormats() as $format => $settings) {
101
                if (MediaProviderInterface::FORMAT_ADMIN === $format ||
102
                    substr($format, 0, \strlen((string) $media->getContext())) === $media->getContext()) {
103
                    $flushPaths[] = $this->getFilesystem()->get($this->generatePrivateUrl($media, $format), true)->getKey();
104
                }
105
            }
106
            if (!empty($flushPaths)) {
107
                $cdnFlushIdentifier = $this->getCdn()->flushPaths($flushPaths);
108
                $media->setCdnFlushIdentifier($cdnFlushIdentifier);
0 ignored issues
show
Documentation introduced by
$cdnFlushIdentifier is of type null|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
                $media->setCdnIsFlushable(true);
110
                $media->setCdnStatus(CDNInterface::STATUS_TO_FLUSH);
111
            }
112
        }
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function addFormat($name, $format): void
119
    {
120
        $this->formats[$name] = $format;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getFormat($name)
127
    {
128
        return isset($this->formats[$name]) ? $this->formats[$name] : false;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function requireThumbnails()
135
    {
136
        return null !== $this->getResizer();
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function generateThumbnails(MediaInterface $media): void
143
    {
144
        $this->thumbnail->generate($this, $media);
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function removeThumbnails(MediaInterface $media, $formats = null): void
151
    {
152
        $this->thumbnail->delete($this, $media, $formats);
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getFormatName(MediaInterface $media, $format)
159
    {
160
        if (MediaProviderInterface::FORMAT_ADMIN === $format) {
161
            return MediaProviderInterface::FORMAT_ADMIN;
162
        }
163
164
        if (MediaProviderInterface::FORMAT_REFERENCE === $format) {
165
            return MediaProviderInterface::FORMAT_REFERENCE;
166
        }
167
168
        $baseName = $media->getContext().'_';
169
        if (substr($format, 0, \strlen($baseName)) === $baseName) {
170
            return $format;
171
        }
172
173
        return $baseName.$format;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function getProviderMetadata()
180
    {
181
        return new Metadata($this->getName(), $this->getName().'.description', null, 'SonataMediaBundle', ['class' => 'fa fa-file']);
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function preRemove(MediaInterface $media): void
188
    {
189
        $hash = spl_object_hash($media);
190
        $this->clones[$hash] = clone $media;
191
192
        if ($this->requireThumbnails()) {
193
            $this->thumbnail->delete($this, $media);
194
        }
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function postRemove(MediaInterface $media): void
201
    {
202
        $hash = spl_object_hash($media);
203
204
        if (isset($this->clones[$hash])) {
205
            $media = $this->clones[$hash];
206
            unset($this->clones[$hash]);
207
        }
208
209
        $path = $this->getReferenceImage($media);
210
211
        if ($this->getFilesystem()->has($path)) {
212
            $this->getFilesystem()->delete($path);
213
        }
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function generatePath(MediaInterface $media)
220
    {
221
        return $this->pathGenerator->generatePath($media);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function getFormats()
228
    {
229
        return $this->formats;
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function setName($name): void
236
    {
237
        $this->name = $name;
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function getName()
244
    {
245
        return $this->name;
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function setTemplates(array $templates): void
252
    {
253
        $this->templates = $templates;
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function getTemplates()
260
    {
261
        return $this->templates;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function getTemplate($name)
268
    {
269
        return isset($this->templates[$name]) ? $this->templates[$name] : null;
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function getResizer()
276
    {
277
        return $this->resizer;
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function getFilesystem()
284
    {
285
        return $this->filesystem;
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function getCdn()
292
    {
293
        return $this->cdn;
294
    }
295
296
    /**
297
     * {@inheritdoc}
298
     */
299
    public function getCdnPath($relativePath, $isFlushable)
300
    {
301
        return $this->getCdn()->getPath($relativePath, $isFlushable);
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307
    public function setResizer(ResizerInterface $resizer): void
308
    {
309
        $this->resizer = $resizer;
310
    }
311
312
    /**
313
     * {@inheritdoc}
314
     */
315
    public function prePersist(MediaInterface $media): void
316
    {
317
        $media->setCreatedAt(new \DateTime());
318
        $media->setUpdatedAt(new \DateTime());
319
    }
320
321
    /**
322
     * {@inheritdoc}
323
     */
324
    public function preUpdate(MediaInterface $media): void
325
    {
326
        $media->setUpdatedAt(new \DateTime());
327
    }
328
329
    /**
330
     * {@inheritdoc}
331
     */
332
    public function validate(ErrorElement $errorElement, MediaInterface $media): void
333
    {
334
    }
335
336
    abstract protected function doTransform(MediaInterface $media);
337
}
338