1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\Model; |
13
|
|
|
|
14
|
|
|
use Imagine\Image\Box; |
15
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
16
|
|
|
|
17
|
|
|
abstract class Media implements MediaInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $enabled = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $providerName; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $providerStatus; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $providerReference; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $providerMetadata = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
protected $width; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
protected $height; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var float |
66
|
|
|
*/ |
67
|
|
|
protected $length; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $copyright; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $authorName; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
protected $context; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
protected $cdnIsFlushable; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
protected $cdnFlushIdentifier; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var \DateTime |
96
|
|
|
*/ |
97
|
|
|
protected $cdnFlushAt; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var int |
101
|
|
|
*/ |
102
|
|
|
protected $cdnStatus; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var \DateTime |
106
|
|
|
*/ |
107
|
|
|
protected $updatedAt; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var \DateTime |
111
|
|
|
*/ |
112
|
|
|
protected $createdAt; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var mixed |
116
|
|
|
*/ |
117
|
|
|
protected $binaryContent; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var string |
121
|
|
|
*/ |
122
|
|
|
protected $previousProviderReference; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var string |
126
|
|
|
*/ |
127
|
|
|
protected $contentType; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @var int |
131
|
|
|
*/ |
132
|
|
|
protected $size; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @var GalleryItemInterface[] |
136
|
|
|
*/ |
137
|
|
|
protected $galleryItems; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @var CategoryInterface |
141
|
|
|
*/ |
142
|
|
|
protected $category; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function __toString() |
148
|
|
|
{ |
149
|
|
|
return $this->getName() ?: 'n/a'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function prePersist() |
153
|
|
|
{ |
154
|
|
|
$this->setCreatedAt(new \DateTime()); |
155
|
|
|
$this->setUpdatedAt(new \DateTime()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function preUpdate() |
159
|
|
|
{ |
160
|
|
|
$this->setUpdatedAt(new \DateTime()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @static |
165
|
|
|
* |
166
|
|
|
* @return string[] |
167
|
|
|
*/ |
168
|
|
|
public static function getStatusList() |
169
|
|
|
{ |
170
|
|
|
return array( |
171
|
|
|
self::STATUS_OK => 'ok', |
172
|
|
|
self::STATUS_SENDING => 'sending', |
173
|
|
|
self::STATUS_PENDING => 'pending', |
174
|
|
|
self::STATUS_ERROR => 'error', |
175
|
|
|
self::STATUS_ENCODING => 'encoding', |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function setBinaryContent($binaryContent) |
183
|
|
|
{ |
184
|
|
|
$this->previousProviderReference = $this->providerReference; |
185
|
|
|
$this->providerReference = null; |
186
|
|
|
$this->binaryContent = $binaryContent; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function resetBinaryContent() |
193
|
|
|
{ |
194
|
|
|
$this->binaryContent = null; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
|
public function getBinaryContent() |
201
|
|
|
{ |
202
|
|
|
return $this->binaryContent; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function getMetadataValue($name, $default = null) |
209
|
|
|
{ |
210
|
|
|
$metadata = $this->getProviderMetadata(); |
211
|
|
|
|
212
|
|
|
return isset($metadata[$name]) ? $metadata[$name] : $default; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function setMetadataValue($name, $value) |
219
|
|
|
{ |
220
|
|
|
$metadata = $this->getProviderMetadata(); |
221
|
|
|
$metadata[$name] = $value; |
222
|
|
|
$this->setProviderMetadata($metadata); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
*/ |
228
|
|
|
public function unsetMetadataValue($name) |
229
|
|
|
{ |
230
|
|
|
$metadata = $this->getProviderMetadata(); |
231
|
|
|
unset($metadata[$name]); |
232
|
|
|
$this->setProviderMetadata($metadata); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public function setName($name) |
239
|
|
|
{ |
240
|
|
|
$this->name = $name; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
|
|
public function getName() |
247
|
|
|
{ |
248
|
|
|
return $this->name; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritdoc} |
253
|
|
|
*/ |
254
|
|
|
public function setDescription($description) |
255
|
|
|
{ |
256
|
|
|
$this->description = $description; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* {@inheritdoc} |
261
|
|
|
*/ |
262
|
|
|
public function getDescription() |
263
|
|
|
{ |
264
|
|
|
return $this->description; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
|
|
public function setEnabled($enabled) |
271
|
|
|
{ |
272
|
|
|
$this->enabled = $enabled; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* {@inheritdoc} |
277
|
|
|
*/ |
278
|
|
|
public function getEnabled() |
279
|
|
|
{ |
280
|
|
|
return $this->enabled; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* {@inheritdoc} |
285
|
|
|
*/ |
286
|
|
|
public function setProviderName($providerName) |
287
|
|
|
{ |
288
|
|
|
$this->providerName = $providerName; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
|
|
public function getProviderName() |
295
|
|
|
{ |
296
|
|
|
return $this->providerName; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* {@inheritdoc} |
301
|
|
|
*/ |
302
|
|
|
public function setProviderStatus($providerStatus) |
303
|
|
|
{ |
304
|
|
|
$this->providerStatus = $providerStatus; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* {@inheritdoc} |
309
|
|
|
*/ |
310
|
|
|
public function getProviderStatus() |
311
|
|
|
{ |
312
|
|
|
return $this->providerStatus; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* {@inheritdoc} |
317
|
|
|
*/ |
318
|
|
|
public function setProviderReference($providerReference) |
319
|
|
|
{ |
320
|
|
|
$this->providerReference = $providerReference; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* {@inheritdoc} |
325
|
|
|
*/ |
326
|
|
|
public function getProviderReference() |
327
|
|
|
{ |
328
|
|
|
return $this->providerReference; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* {@inheritdoc} |
333
|
|
|
*/ |
334
|
|
|
public function setProviderMetadata(array $providerMetadata = array()) |
335
|
|
|
{ |
336
|
|
|
$this->providerMetadata = $providerMetadata; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* {@inheritdoc} |
341
|
|
|
*/ |
342
|
|
|
public function getProviderMetadata() |
343
|
|
|
{ |
344
|
|
|
return $this->providerMetadata; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* {@inheritdoc} |
349
|
|
|
*/ |
350
|
|
|
public function setWidth($width) |
351
|
|
|
{ |
352
|
|
|
$this->width = $width; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* {@inheritdoc} |
357
|
|
|
*/ |
358
|
|
|
public function getWidth() |
359
|
|
|
{ |
360
|
|
|
return $this->width; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* {@inheritdoc} |
365
|
|
|
*/ |
366
|
|
|
public function setHeight($height) |
367
|
|
|
{ |
368
|
|
|
$this->height = $height; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* {@inheritdoc} |
373
|
|
|
*/ |
374
|
|
|
public function getHeight() |
375
|
|
|
{ |
376
|
|
|
return $this->height; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* {@inheritdoc} |
381
|
|
|
*/ |
382
|
|
|
public function setLength($length) |
383
|
|
|
{ |
384
|
|
|
$this->length = $length; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* {@inheritdoc} |
389
|
|
|
*/ |
390
|
|
|
public function getLength() |
391
|
|
|
{ |
392
|
|
|
return $this->length; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* {@inheritdoc} |
397
|
|
|
*/ |
398
|
|
|
public function setCopyright($copyright) |
399
|
|
|
{ |
400
|
|
|
$this->copyright = $copyright; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* {@inheritdoc} |
405
|
|
|
*/ |
406
|
|
|
public function getCopyright() |
407
|
|
|
{ |
408
|
|
|
return $this->copyright; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* {@inheritdoc} |
413
|
|
|
*/ |
414
|
|
|
public function setAuthorName($authorName) |
415
|
|
|
{ |
416
|
|
|
$this->authorName = $authorName; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* {@inheritdoc} |
421
|
|
|
*/ |
422
|
|
|
public function getAuthorName() |
423
|
|
|
{ |
424
|
|
|
return $this->authorName; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* {@inheritdoc} |
429
|
|
|
*/ |
430
|
|
|
public function setContext($context) |
431
|
|
|
{ |
432
|
|
|
$this->context = $context; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* {@inheritdoc} |
437
|
|
|
*/ |
438
|
|
|
public function getContext() |
439
|
|
|
{ |
440
|
|
|
return $this->context; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* {@inheritdoc} |
445
|
|
|
*/ |
446
|
|
|
public function setCdnIsFlushable($cdnIsFlushable) |
447
|
|
|
{ |
448
|
|
|
$this->cdnIsFlushable = $cdnIsFlushable; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* {@inheritdoc} |
453
|
|
|
*/ |
454
|
|
|
public function getCdnIsFlushable() |
455
|
|
|
{ |
456
|
|
|
return $this->cdnIsFlushable; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* {@inheritdoc} |
461
|
|
|
*/ |
462
|
|
|
public function setCdnFlushIdentifier($cdnFlushIdentifier) |
463
|
|
|
{ |
464
|
|
|
$this->cdnFlushIdentifier = $cdnFlushIdentifier; |
|
|
|
|
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* {@inheritdoc} |
469
|
|
|
*/ |
470
|
|
|
public function getCdnFlushIdentifier() |
471
|
|
|
{ |
472
|
|
|
return $this->cdnFlushIdentifier; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* {@inheritdoc} |
477
|
|
|
*/ |
478
|
|
|
public function setCdnFlushAt(\DateTime $cdnFlushAt = null) |
479
|
|
|
{ |
480
|
|
|
$this->cdnFlushAt = $cdnFlushAt; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* {@inheritdoc} |
485
|
|
|
*/ |
486
|
|
|
public function getCdnFlushAt() |
487
|
|
|
{ |
488
|
|
|
return $this->cdnFlushAt; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* {@inheritdoc} |
493
|
|
|
*/ |
494
|
|
|
public function setUpdatedAt(\DateTime $updatedAt = null) |
495
|
|
|
{ |
496
|
|
|
$this->updatedAt = $updatedAt; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* {@inheritdoc} |
501
|
|
|
*/ |
502
|
|
|
public function getUpdatedAt() |
503
|
|
|
{ |
504
|
|
|
return $this->updatedAt; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* {@inheritdoc} |
509
|
|
|
*/ |
510
|
|
|
public function setCreatedAt(\DateTime $createdAt = null) |
511
|
|
|
{ |
512
|
|
|
$this->createdAt = $createdAt; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* {@inheritdoc} |
517
|
|
|
*/ |
518
|
|
|
public function getCreatedAt() |
519
|
|
|
{ |
520
|
|
|
return $this->createdAt; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* {@inheritdoc} |
525
|
|
|
*/ |
526
|
|
|
public function setContentType($contentType) |
527
|
|
|
{ |
528
|
|
|
$this->contentType = $contentType; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* {@inheritdoc} |
533
|
|
|
*/ |
534
|
|
|
public function getContentType() |
535
|
|
|
{ |
536
|
|
|
return $this->contentType; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* {@inheritdoc} |
541
|
|
|
*/ |
542
|
|
|
public function getExtension() |
543
|
|
|
{ |
544
|
|
|
// strips off query strings or hashes, which are common in URIs remote references |
545
|
|
|
return preg_replace('{(\?|#).*}', '', pathinfo($this->getProviderReference(), PATHINFO_EXTENSION)); |
|
|
|
|
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* {@inheritdoc} |
550
|
|
|
*/ |
551
|
|
|
public function setSize($size) |
552
|
|
|
{ |
553
|
|
|
$this->size = $size; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* {@inheritdoc} |
558
|
|
|
*/ |
559
|
|
|
public function getSize() |
560
|
|
|
{ |
561
|
|
|
return $this->size; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* {@inheritdoc} |
566
|
|
|
*/ |
567
|
|
|
public function setCdnStatus($cdnStatus) |
568
|
|
|
{ |
569
|
|
|
$this->cdnStatus = $cdnStatus; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* {@inheritdoc} |
574
|
|
|
*/ |
575
|
|
|
public function getCdnStatus() |
576
|
|
|
{ |
577
|
|
|
return $this->cdnStatus; |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* {@inheritdoc} |
582
|
|
|
*/ |
583
|
|
|
public function getBox() |
584
|
|
|
{ |
585
|
|
|
return new Box($this->width, $this->height); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
/** |
589
|
|
|
* {@inheritdoc} |
590
|
|
|
*/ |
591
|
|
|
public function setGalleryItems($galleryItems) |
592
|
|
|
{ |
593
|
|
|
$this->galleryItems = $galleryItems; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* {@inheritdoc} |
598
|
|
|
*/ |
599
|
|
|
public function getGalleryItems() |
600
|
|
|
{ |
601
|
|
|
return $this->galleryItems; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* {@inheritdoc} |
606
|
|
|
*/ |
607
|
|
|
public function getPreviousProviderReference() |
608
|
|
|
{ |
609
|
|
|
return $this->previousProviderReference; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* @param ExecutionContextInterface $context |
614
|
|
|
*/ |
615
|
|
|
public function isStatusErroneous(ExecutionContextInterface $context) |
616
|
|
|
{ |
617
|
|
|
if ($this->getBinaryContent() && $this->getProviderStatus() == self::STATUS_ERROR) { |
618
|
|
|
$context->buildViolation('invalid') |
619
|
|
|
->atPath('binaryContent') |
620
|
|
|
->addViolation(); |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* @return CategoryInterface |
626
|
|
|
*/ |
627
|
|
|
public function getCategory() |
628
|
|
|
{ |
629
|
|
|
return $this->category; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* @param CategoryInterface|null $category |
634
|
|
|
*/ |
635
|
|
|
public function setCategory($category = null) |
636
|
|
|
{ |
637
|
|
|
if (null !== $category && !is_a($category, 'Sonata\ClassificationBundle\Model\CategoryInterface')) { |
638
|
|
|
throw new \InvalidArgumentException( |
639
|
|
|
'$category should be an instance of Sonata\ClassificationBundle\Model\CategoryInterface or null' |
640
|
|
|
); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
$this->category = $category; |
644
|
|
|
} |
645
|
|
|
} |
646
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.