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

Media   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 62.96%

Importance

Changes 6
Bugs 2 Features 4
Metric Value
eloc 19
dl 0
loc 118
ccs 17
cts 27
cp 0.6296
rs 10
c 6
b 2
f 4
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setCollection() 0 3 1
A __toString() 0 3 1
A getFullUrl() 0 3 1
A getExtension() 0 3 1
A isDefault() 0 3 1
A getUrl() 0 7 2
A setModel() 0 3 1
A getCollection() 0 3 1
A getModel() 0 3 1
A getBasePath() 0 7 2
1
<?php
2
3
namespace ByTIC\MediaLibrary\Media;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use ByTIC\MediaLibrary\Collections\Traits\LoadMediaTrait;
7
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
8
use ByTIC\MediaLibrary\PathGenerator\PathGeneratorFactory;
9
use Nip\Records\Record;
10
use function Nip\url;
11
12
/**
13
 * Class Media.
14
 */
15
class Media
16
{
17
    use Traits\FileMethodsTrait;
18
    use Traits\HasConversionsTrait;
19
20
    /**
21
     * @var Record
22
     */
23
    protected $model;
24
25
    /**
26
     * @var Collection
27
     */
28
    protected $collection;
29
30
    /**
31
     * @return Record|HasMediaTrait
32
     */
33 8
    public function getModel(): Record
34
    {
35 8
        return $this->model;
36
    }
37
38
    /**
39
     * @param Record|HasMediaTrait $record
40
     */
41 9
    public function setModel(Record $record)
42
    {
43 9
        $this->model = $record;
44 9
    }
45
46
    /**
47
     * @return string
48
     */
49 4
    public function getExtension()
50
    {
51 4
        return pathinfo($this->getName(), PATHINFO_EXTENSION);
52
    }
53
54
    /**
55
     * @param string $conversionName
56
     * @return string
57
     */
58 2
    public function getBasePath(string $conversionName = '')
59
    {
60 2
        $path = PathGeneratorFactory::create()::getBasePathForMedia($this);
61 2
        if ($conversionName) {
62 2
            $path = $path . DIRECTORY_SEPARATOR . $conversionName;
63
        }
64 2
        return $path;
65
    }
66
67
    /**
68
     * @return string
69
     *
70
     * @deprecated Use getFullUrl
71
     */
72
    public function __toString()
73
    {
74
        return $this->getFullUrl();
75
    }
76
77
    /**
78
     * Get the full url to a original media file.
79
     *
80
     * @param string $conversionName
81
     *
82
     * @return string
83
     */
84
    public function getFullUrl(string $conversionName = ''): string
85
    {
86
        return url()->to($this->getUrl($conversionName));
87
    }
88
89
    /**
90
     * Get the url to a original media file.
91
     *
92
     * @param string $conversionName
93
     *
94
     * @return string
95
     */
96
    public function getUrl(string $conversionName = ''): string
0 ignored issues
show
Unused Code introduced by
The parameter $conversionName is not used and could be removed. ( Ignorable by Annotation )

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

96
    public function getUrl(/** @scrutinizer ignore-unused */ string $conversionName = ''): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
    {
98
        if ($this->hasFile()) {
99
            return $this->getFile()->getUrl();
100
        }
101
102
        return $this->getCollection()->getDefaultMediaUrl();
103
//        $urlGenerator = UrlGeneratorFactory::createForMedia($this);
104
//        if ($conversionName !== '') {
105
////            $conversion = ConversionCollection::createForMedia($this)->getByName($conversionName);
106
////            $urlGenerator->setConversion($conversion);
107
//        }
108
//        return $urlGenerator->getUrl();
109
    }
110
111
    /**
112
     * @return Collection
113
     */
114 8
    public function getCollection(): Collection
115
    {
116 8
        return $this->collection;
117
    }
118
119
    /**
120
     * @param Collection|LoadMediaTrait $collection
121
     */
122 8
    public function setCollection(Collection $collection)
123
    {
124 8
        $this->collection = $collection;
125 8
    }
126
127
    /**
128
     * @return bool
129
     */
130
    public function isDefault()
131
    {
132
        return $this === $this->getCollection()->getDefaultMedia();
133
    }
134
}
135