Completed
Push — master ( 93e21a...f3a82e )
by Christian
03:39
created

Media::isStatusErroneous()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
622
                throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\Component\Validator\ExecutionContextInterface or Symfony\Component\Validator\Context\ExecutionContextInterface');
623
            }
624
625
            if ($context instanceof LegacyExecutionContextInterface) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Valida...ecutionContextInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
626
                $context->addViolationAt('binaryContent', 'invalid', array(), null);
627
            } else {
628
                $context->buildViolation('invalid')
629
                   ->atPath('binaryContent')
630
                   ->addViolation();
631
            }
632
        }
633
    }
634
635
    /**
636
     * @return CategoryInterface
637
     */
638
    public function getCategory()
639
    {
640
        return $this->category;
641
    }
642
643
    /**
644
     * @param CategoryInterface $category|null
0 ignored issues
show
Documentation introduced by
There is no parameter named $category|null. Did you maybe mean $category?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
645
     */
646
    public function setCategory(CategoryInterface $category = null)
647
    {
648
        $this->category = $category;
649
    }
650
}
651