Passed
Push — master ( c6f9a3...c9f7be )
by Gabriel
03:32
created

HasMediaConversionsTrait::initMediaConversions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\HasMedia\Traits;
4
5
use ByTIC\MediaLibrary\Conversions\Conversion;
6
use ByTIC\MediaLibrary\Conversions\ConversionCollection;
7
8
/**
9
 * Trait HasMediaConversionsTrait.
10
 */
11
trait HasMediaConversionsTrait
12
{
13
    public $mediaConversions = null;
14
15
    /**
16
     * Add a conversion.
17
     *
18
     * @param string $name
19
     *
20
     * @return Conversion
21
     */
22 3
    public function addMediaConversion(string $name): Conversion
23
    {
24 3
        $conversion = Conversion::create($name);
25 3
        $this->mediaConversions[$name] = $conversion;
26
27 3
        return $conversion;
28
    }
29
30
    /**
31
     * @return ConversionCollection
32
     */
33 5
    public function getMediaConversions() : ConversionCollection
34
    {
35 5
        if ($this->mediaConversions === null) {
36 5
            $this->initMediaConversions();
37
        }
38
39 5
        return $this->mediaConversions;
40
    }
41
42
    /**
43
     * @return bool
44
     */
45 1
    public function hasMediaConversions()
46
    {
47 1
        return $this->getMediaConversions()->isNotEmpty();
48
    }
49
50 5
    protected function initMediaConversions()
51
    {
52 5
        $this->mediaConversions = new ConversionCollection();
53
54 5
        if (method_exists($this, 'registerMediaConversions')) {
55 1
            $this->registerMediaConversions();
56
        }
57 5
    }
58
}
59