Passed
Push — master ( f972fc...fc48d8 )
by Gabriel
03:18
created

HasConversionsTrait::hasConversion()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 4
rs 10
1
<?php
2
3
namespace ByTIC\MediaLibrary\Media\Traits;
4
5
use ByTIC\MediaLibrary\Conversions\ConversionCollection;
6
7
/**
8
 * Trait HasConversionsTrait
9
 * @package ByTIC\MediaLibrary\Media\Traits
10
 */
11
trait HasConversionsTrait
12
{
13
    protected $conversions = null;
14
15
    protected $conversionNames;
16
17 4
    public function getConversionNames(): array
18
    {
19 4
        if ($this->conversionNames == null) {
20 4
            $this->conversionNames = $this->generateConversionNames();
21
        }
22 4
        return $this->conversionNames;
23
    }
24
25
    /**
26
     * @return array
27
     */
28 4
    protected function generateConversionNames()
29
    {
30 4
        $conversions = $this->getConversions();
31
32 4
        $return = [];
33 4
        foreach ($conversions as $conversion) {
34 2
            $return[] = $conversion->getName();
35
        }
36 4
        return $return;
37
    }
38
39 4
    public function getConversions(): ConversionCollection
40
    {
41 4
        if ($this->conversions == null) {
42 4
            $this->conversions = $this->generateConversions();
43
        }
44 4
        return $this->conversions;
45
    }
46
47
    /**
48
     * @param $name
49
     * @return bool
50
     */
51 1
    public function hasConversion($name)
52
    {
53 1
        $names = is_array($name) ? $name : [$name];
54 1
        $conversions = $this->getConversionNames();
55 1
        foreach ($names as $name) {
0 ignored issues
show
introduced by
$name is overwriting one of the parameters of this function.
Loading history...
56 1
            if (!in_array($name, $conversions)) {
57 1
                return false;
58
            }
59
        }
60 1
        return true;
61
    }
62
63
    /**
64
     * @return ConversionCollection
65
     */
66 4
    protected function generateConversions()
67
    {
68 4
        $collection = $this->getCollection();
0 ignored issues
show
Bug introduced by
It seems like getCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        /** @scrutinizer ignore-call */ 
69
        $collection = $this->getCollection();
Loading history...
69 4
        $model = $this->getModel();
0 ignored issues
show
Bug introduced by
It seems like getModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        /** @scrutinizer ignore-call */ 
70
        $model = $this->getModel();
Loading history...
70 4
        if ($collection && $model) {
71 4
            return $model->getMediaConversions()->forCollection($collection->getName());
72
        }
73
        return new ConversionCollection();
74
    }
75
76
77 2
    public function removeConversions()
78
    {
79 2
        $converstions = $this->getConversionNames();
80 2
        $converstions[] = 'full';
81 2
        $filesystem = $this->getFile()->getFilesystem();
0 ignored issues
show
Bug introduced by
It seems like getFile() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        $filesystem = $this->/** @scrutinizer ignore-call */ getFile()->getFilesystem();
Loading history...
82 2
        foreach ($converstions as $converstion) {
83 2
            $path = $this->getPath($converstion);
0 ignored issues
show
Bug introduced by
It seems like getPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
            /** @scrutinizer ignore-call */ 
84
            $path = $this->getPath($converstion);
Loading history...
84 2
            if ($filesystem->has($path)) {
85
                $filesystem->delete($path);
86
            }
87
        }
88 2
    }
89
}
90