Media   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 6
Bugs 2 Features 4
Metric Value
eloc 17
c 6
b 2
f 4
dl 0
loc 85
ccs 20
cts 24
cp 0.8333
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setCollection() 0 3 1
A __toString() 0 3 1
A getExtension() 0 3 1
A isDefault() 0 3 1
A getBasePath() 0 7 2
A setModel() 0 3 1
A getCollection() 0 3 1
A getModel() 0 3 1
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 1
    use Traits\UrlMethodsTrait;
18 1
    use Traits\FileMethodsTrait;
19 1
    use Traits\HasConversionsTrait;
20
21
    /**
22
     * @var Record
23
     */
24
    protected $model;
25
26
    /**
27
     * @var Collection
28
     */
29
    protected $collection;
30
31
    /**
32
     * @return Record|HasMediaTrait
33
     */
34 10
    public function getModel(): Record
35
    {
36 10
        return $this->model;
37
    }
38
39
    /**
40
     * @param Record|HasMediaTrait $record
41
     */
42 11
    public function setModel(Record $record)
43
    {
44 11
        $this->model = $record;
45 11
    }
46
47
    /**
48
     * @return string
49
     */
50 4
    public function getExtension()
51
    {
52 4
        return pathinfo($this->getName(), PATHINFO_EXTENSION);
0 ignored issues
show
Bug Best Practice introduced by
The expression return pathinfo($this->g...dia\PATHINFO_EXTENSION) also could return the type array which is incompatible with the documented return type string.
Loading history...
53
    }
54
55
    /**
56
     * @param string $conversionName
57
     * @return string
58
     */
59 2
    public function getBasePath(string $conversionName = '')
60
    {
61 2
        $path = PathGeneratorFactory::create()::getBasePathForMedia($this);
62 2
        if ($conversionName) {
63 2
            $path = rtrim($path, '/') . '/' . $conversionName;
64
        }
65 2
        return $path;
66
    }
67
68
    /**
69
     * @return string
70
     *
71
     * @deprecated Use getFullUrl
72
     */
73
    public function __toString()
74
    {
75
        return $this->getFullUrl();
76
    }
77
78
    /**
79
     * @return Collection
80
     */
81 10
    public function getCollection(): Collection
82
    {
83 10
        return $this->collection;
84
    }
85
86
    /**
87
     * @param Collection|LoadMediaTrait $collection
88
     */
89 10
    public function setCollection(Collection $collection)
90
    {
91 10
        $this->collection = $collection;
92 10
    }
93
94
    /**
95
     * @return bool
96
     */
97
    public function isDefault()
98
    {
99
        return $this->getPath() === $this->getCollection()->getDefaultMedia()->getPath();
100
    }
101
}
102