Passed
Push — master ( 859573...a81ea2 )
by Gabriel
03:21
created

HasConversionsTrait::getConversions()   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 1
Bugs 0 Features 1
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 1
b 0
f 1
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 3
    public function getConversionNames(): array
18
    {
19 3
        if ($this->conversionNames == null) {
20 3
            $this->conversionNames = $this->generateConversionNames();
21
        }
22 3
        return $this->conversionNames;
23
    }
24
25
    /**
26
     * @return array
27
     */
28 3
    protected function generateConversionNames()
29
    {
30 3
        $conversions = $this->getConversions();
31
32 3
        $return = [];
33 3
        foreach ($conversions as $conversion) {
34 1
            $return[] = $conversion->getName();
35
        }
36 3
        return $return;
37
    }
38
39 3
    public function getConversions(): ConversionCollection
40
    {
41 3
        if ($this->conversions == null) {
42 3
            $this->conversions = $this->generateConversions();
43
        }
44 3
        return $this->conversions;
45
    }
46
47
    /**
48
     * @return ConversionCollection
49
     */
50 3
    protected function generateConversions()
51
    {
52 3
        $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

52
        /** @scrutinizer ignore-call */ 
53
        $collection = $this->getCollection();
Loading history...
53 3
        $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

53
        /** @scrutinizer ignore-call */ 
54
        $model = $this->getModel();
Loading history...
54 3
        if ($collection && $model) {
55 3
            return $model->getMediaConversions()->forCollection($collection->getName());
56
        }
57
        return new ConversionCollection();
58
    }
59
60
61 2
    public function removeConversions()
62
    {
63 2
        $converstions = $this->getConversionNames();
64 2
        $converstions[] = 'full';
65 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

65
        $filesystem = $this->/** @scrutinizer ignore-call */ getFile()->getFilesystem();
Loading history...
66 2
        foreach ($converstions as $converstion) {
67 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

67
            /** @scrutinizer ignore-call */ 
68
            $path = $this->getPath($converstion);
Loading history...
68 2
            if ($filesystem->has($path)) {
69
                $filesystem->delete($path);
70
            }
71
        }
72 2
    }
73
}
74