Completed
Push — master ( 8c0a67...1fafee )
by Grégoire
9s
created

Media::resetBinaryContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 Sonata\ClassificationBundle\Model\CategoryInterface;
16
use Symfony\Component\Validator\Constraints as Assert;
17
use Symfony\Component\Validator\Context\ExecutionContextInterface;
18
use Symfony\Component\Validator\Mapping\ClassMetadata;
19
20
abstract class Media implements MediaInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $name;
26
27
    /**
28
     * @var string
29
     */
30
    protected $description;
31
32
    /**
33
     * @var bool
34
     */
35
    protected $enabled = false;
36
37
    /**
38
     * @var string
39
     */
40
    protected $providerName;
41
42
    /**
43
     * @var int
44
     */
45
    protected $providerStatus;
46
47
    /**
48
     * @var string
49
     */
50
    protected $providerReference;
51
52
    /**
53
     * @var array
54
     */
55
    protected $providerMetadata = array();
56
57
    /**
58
     * @var int
59
     */
60
    protected $width;
61
62
    /**
63
     * @var int
64
     */
65
    protected $height;
66
67
    /**
68
     * @var float
69
     */
70
    protected $length;
71
72
    /**
73
     * @var string
74
     */
75
    protected $copyright;
76
77
    /**
78
     * @var string
79
     */
80
    protected $authorName;
81
82
    /**
83
     * @var string
84
     */
85
    protected $context;
86
87
    /**
88
     * @var bool
89
     */
90
    protected $cdnIsFlushable;
91
92
    /**
93
     * @var string
94
     */
95
    protected $cdnFlushIdentifier;
96
97
    /**
98
     * @var \DateTime
99
     */
100
    protected $cdnFlushAt;
101
102
    /**
103
     * @var int
104
     */
105
    protected $cdnStatus;
106
107
    /**
108
     * @var \DateTime
109
     */
110
    protected $updatedAt;
111
112
    /**
113
     * @var \DateTime
114
     */
115
    protected $createdAt;
116
117
    /**
118
     * @var mixed
119
     */
120
    protected $binaryContent;
121
122
    /**
123
     * @var string
124
     */
125
    protected $previousProviderReference;
126
127
    /**
128
     * @var string
129
     */
130
    protected $contentType;
131
132
    /**
133
     * @var int
134
     */
135
    protected $size;
136
137
    /**
138
     * @var GalleryItemInterface[]
139
     */
140
    protected $galleryItems;
141
142
    /**
143
     * @var CategoryInterface
144
     */
145
    protected $category;
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function __toString()
151
    {
152
        return $this->getName() ?: 'n/a';
153
    }
154
155
    public function prePersist()
156
    {
157
        $this->setCreatedAt(new \DateTime());
158
        $this->setUpdatedAt(new \DateTime());
159
    }
160
161
    public function preUpdate()
162
    {
163
        $this->setUpdatedAt(new \DateTime());
164
    }
165
166
    /**
167
     * @static
168
     *
169
     * @return string[]
170
     */
171
    public static function getStatusList()
172
    {
173
        return array(
174
            self::STATUS_OK => 'ok',
175
            self::STATUS_SENDING => 'sending',
176
            self::STATUS_PENDING => 'pending',
177
            self::STATUS_ERROR => 'error',
178
            self::STATUS_ENCODING => 'encoding',
179
        );
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function setBinaryContent($binaryContent)
186
    {
187
        $this->previousProviderReference = $this->providerReference;
188
        $this->providerReference = null;
189
        $this->binaryContent = $binaryContent;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function resetBinaryContent()
196
    {
197
        $this->binaryContent = null;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function getBinaryContent()
204
    {
205
        return $this->binaryContent;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function getMetadataValue($name, $default = null)
212
    {
213
        $metadata = $this->getProviderMetadata();
214
215
        return isset($metadata[$name]) ? $metadata[$name] : $default;
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221
    public function setMetadataValue($name, $value)
222
    {
223
        $metadata = $this->getProviderMetadata();
224
        $metadata[$name] = $value;
225
        $this->setProviderMetadata($metadata);
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function unsetMetadataValue($name)
232
    {
233
        $metadata = $this->getProviderMetadata();
234
        unset($metadata[$name]);
235
        $this->setProviderMetadata($metadata);
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241
    public function setName($name)
242
    {
243
        $this->name = $name;
244
    }
245
246
    /**
247
     * {@inheritdoc}
248
     */
249
    public function getName()
250
    {
251
        return $this->name;
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function setDescription($description)
258
    {
259
        $this->description = $description;
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function getDescription()
266
    {
267
        return $this->description;
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273
    public function setEnabled($enabled)
274
    {
275
        $this->enabled = $enabled;
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281
    public function getEnabled()
282
    {
283
        return $this->enabled;
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function setProviderName($providerName)
290
    {
291
        $this->providerName = $providerName;
292
    }
293
294
    /**
295
     * {@inheritdoc}
296
     */
297
    public function getProviderName()
298
    {
299
        return $this->providerName;
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305
    public function setProviderStatus($providerStatus)
306
    {
307
        $this->providerStatus = $providerStatus;
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313
    public function getProviderStatus()
314
    {
315
        return $this->providerStatus;
316
    }
317
318
    /**
319
     * {@inheritdoc}
320
     */
321
    public function setProviderReference($providerReference)
322
    {
323
        $this->providerReference = $providerReference;
324
    }
325
326
    /**
327
     * {@inheritdoc}
328
     */
329
    public function getProviderReference()
330
    {
331
        return $this->providerReference;
332
    }
333
334
    /**
335
     * {@inheritdoc}
336
     */
337
    public function setProviderMetadata(array $providerMetadata = array())
338
    {
339
        $this->providerMetadata = $providerMetadata;
340
    }
341
342
    /**
343
     * {@inheritdoc}
344
     */
345
    public function getProviderMetadata()
346
    {
347
        return $this->providerMetadata;
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    public function setWidth($width)
354
    {
355
        $this->width = $width;
356
    }
357
358
    /**
359
     * {@inheritdoc}
360
     */
361
    public function getWidth()
362
    {
363
        return $this->width;
364
    }
365
366
    /**
367
     * {@inheritdoc}
368
     */
369
    public function setHeight($height)
370
    {
371
        $this->height = $height;
372
    }
373
374
    /**
375
     * {@inheritdoc}
376
     */
377
    public function getHeight()
378
    {
379
        return $this->height;
380
    }
381
382
    /**
383
     * {@inheritdoc}
384
     */
385
    public function setLength($length)
386
    {
387
        $this->length = $length;
388
    }
389
390
    /**
391
     * {@inheritdoc}
392
     */
393
    public function getLength()
394
    {
395
        return $this->length;
396
    }
397
398
    /**
399
     * {@inheritdoc}
400
     */
401
    public function setCopyright($copyright)
402
    {
403
        $this->copyright = $copyright;
404
    }
405
406
    /**
407
     * {@inheritdoc}
408
     */
409
    public function getCopyright()
410
    {
411
        return $this->copyright;
412
    }
413
414
    /**
415
     * {@inheritdoc}
416
     */
417
    public function setAuthorName($authorName)
418
    {
419
        $this->authorName = $authorName;
420
    }
421
422
    /**
423
     * {@inheritdoc}
424
     */
425
    public function getAuthorName()
426
    {
427
        return $this->authorName;
428
    }
429
430
    /**
431
     * {@inheritdoc}
432
     */
433
    public function setContext($context)
434
    {
435
        $this->context = $context;
436
    }
437
438
    /**
439
     * {@inheritdoc}
440
     */
441
    public function getContext()
442
    {
443
        return $this->context;
444
    }
445
446
    /**
447
     * {@inheritdoc}
448
     */
449
    public function setCdnIsFlushable($cdnIsFlushable)
450
    {
451
        $this->cdnIsFlushable = $cdnIsFlushable;
452
    }
453
454
    /**
455
     * {@inheritdoc}
456
     */
457
    public function getCdnIsFlushable()
458
    {
459
        return $this->cdnIsFlushable;
460
    }
461
462
    /**
463
     * {@inheritdoc}
464
     */
465
    public function setCdnFlushIdentifier($cdnFlushIdentifier)
466
    {
467
        $this->cdnFlushIdentifier = $cdnFlushIdentifier;
0 ignored issues
show
Documentation Bug introduced by
The property $cdnFlushIdentifier was declared of type string, but $cdnFlushIdentifier is of type boolean. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
468
    }
469
470
    /**
471
     * {@inheritdoc}
472
     */
473
    public function getCdnFlushIdentifier()
474
    {
475
        return $this->cdnFlushIdentifier;
476
    }
477
478
    /**
479
     * {@inheritdoc}
480
     */
481
    public function setCdnFlushAt(\DateTime $cdnFlushAt = null)
482
    {
483
        $this->cdnFlushAt = $cdnFlushAt;
484
    }
485
486
    /**
487
     * {@inheritdoc}
488
     */
489
    public function getCdnFlushAt()
490
    {
491
        return $this->cdnFlushAt;
492
    }
493
494
    /**
495
     * {@inheritdoc}
496
     */
497
    public function setUpdatedAt(\DateTime $updatedAt = null)
498
    {
499
        $this->updatedAt = $updatedAt;
500
    }
501
502
    /**
503
     * {@inheritdoc}
504
     */
505
    public function getUpdatedAt()
506
    {
507
        return $this->updatedAt;
508
    }
509
510
    /**
511
     * {@inheritdoc}
512
     */
513
    public function setCreatedAt(\DateTime $createdAt = null)
514
    {
515
        $this->createdAt = $createdAt;
516
    }
517
518
    /**
519
     * {@inheritdoc}
520
     */
521
    public function getCreatedAt()
522
    {
523
        return $this->createdAt;
524
    }
525
526
    /**
527
     * {@inheritdoc}
528
     */
529
    public function setContentType($contentType)
530
    {
531
        $this->contentType = $contentType;
532
    }
533
534
    /**
535
     * {@inheritdoc}
536
     */
537
    public function getContentType()
538
    {
539
        return $this->contentType;
540
    }
541
542
    /**
543
     * {@inheritdoc}
544
     */
545
    public function getExtension()
546
    {
547
        // strips off query strings or hashes, which are common in URIs remote references
548
        return preg_replace('{(\?|#).*}', '', pathinfo($this->getProviderReference(), PATHINFO_EXTENSION));
0 ignored issues
show
Bug Compatibility introduced by
The expression preg_replace('{(\\?|#).*..., PATHINFO_EXTENSION)); of type string[]|string adds the type string[] to the return on line 548 which is incompatible with the return type declared by the interface Sonata\MediaBundle\Model...Interface::getExtension of type string.
Loading history...
549
    }
550
551
    /**
552
     * {@inheritdoc}
553
     */
554
    public function setSize($size)
555
    {
556
        $this->size = $size;
557
    }
558
559
    /**
560
     * {@inheritdoc}
561
     */
562
    public function getSize()
563
    {
564
        return $this->size;
565
    }
566
567
    /**
568
     * {@inheritdoc}
569
     */
570
    public function setCdnStatus($cdnStatus)
571
    {
572
        $this->cdnStatus = $cdnStatus;
573
    }
574
575
    /**
576
     * {@inheritdoc}
577
     */
578
    public function getCdnStatus()
579
    {
580
        return $this->cdnStatus;
581
    }
582
583
    /**
584
     * {@inheritdoc}
585
     */
586
    public function getBox()
587
    {
588
        return new Box($this->width, $this->height);
589
    }
590
591
    /**
592
     * {@inheritdoc}
593
     */
594
    public function setGalleryItems($galleryItems)
595
    {
596
        $this->galleryItems = $galleryItems;
597
    }
598
599
    /**
600
     * {@inheritdoc}
601
     */
602
    public function getGalleryItems()
603
    {
604
        return $this->galleryItems;
605
    }
606
607
    /**
608
     * {@inheritdoc}
609
     */
610
    public function getPreviousProviderReference()
611
    {
612
        return $this->previousProviderReference;
613
    }
614
615
    /**
616
     * NEXT_MAJOR: Remove this method when bumping Symfony requirement to 2.8+.
617
     *
618
     * @param ClassMetadata $metadata
619
     */
620
    public static function loadValidatorMetadata(ClassMetadata $metadata)
621
    {
622
        if (class_exists('Symfony\Component\Validator\Constraints\Expression')) {
623
            $method = 'isStatusErroneous';
624
        } else {
625
            $method = array('methods' => array('isStatusErroneous'));
626
        }
627
        $metadata->addConstraint(new Assert\Callback($method));
628
    }
629
630
    /**
631
     * @param ExecutionContextInterface $context
632
     */
633
    public function isStatusErroneous(ExecutionContextInterface $context)
634
    {
635
        if ($this->getBinaryContent() && $this->getProviderStatus() == self::STATUS_ERROR) {
636
            $context->buildViolation('invalid')
637
               ->atPath('binaryContent')
638
               ->addViolation();
639
        }
640
    }
641
642
    /**
643
     * @return CategoryInterface
644
     */
645
    public function getCategory()
646
    {
647
        return $this->category;
648
    }
649
650
    /**
651
     * @param CategoryInterface|null $category
652
     */
653
    public function setCategory(CategoryInterface $category = null)
654
    {
655
        $this->category = $category;
656
    }
657
}
658