AbstractUrlGenerator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 30%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 17
c 3
b 2
f 0
dl 0
loc 69
ccs 6
cts 20
cp 0.3
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 4 2
A setMedia() 0 5 1
A setPathGenerator() 0 5 1
A getDisk() 0 4 1
A __toString() 0 3 1
A setConversion() 0 5 1
A getUrl() 0 7 2
1
<?php
2
3
namespace ByTIC\MediaLibrary\UrlGenerator;
4
5
use ByTIC\MediaLibrary\Conversions\Conversion;
6
use ByTIC\MediaLibrary\Media\Media;
7
use ByTIC\MediaLibrary\PathGenerator\AbstractPathGenerator;
8
use Nip\Filesystem\FileDisk;
9
10
/**
11
 * Class AbstractUrlGenerator
12
 * @package ByTIC\MediaLibrary\UrlGenerator
13
 */
14
class AbstractUrlGenerator implements UrlGeneratorInterface
15
{
16
    /**
17
     * @var Media
18
     */
19
    protected $media;
20
21
    /**
22
     * @var Conversion
23
     */
24
    protected $conversion = null;
25
26
    /**
27
     * @var AbstractPathGenerator
28
     */
29
    protected $pathGenerator;
30
31
    public function getUrl(): string
32
    {
33
        if (!$this->media->hasFile()) {
34
            return $this->media->getCollection()->getDefaultMediaUrl();
35
        }
36
37
        return $this->getDisk()->getUrl($this->getPath());
38
    }
39
40
    public function getPath(): string
41 1
    {
42
        $convestion = $this->conversion instanceof Conversion ? $this->conversion->getName() : '';
0 ignored issues
show
introduced by
$this->conversion is always a sub-type of ByTIC\MediaLibrary\Conversions\Conversion.
Loading history...
43 1
        return $this->media->getPath($convestion);
44
    }
45 1
46
    public function setMedia(Media $media): UrlGeneratorInterface
47
    {
48
        $this->media = $media;
49
50
        return $this;
51
    }
52
53
    public function setConversion(Conversion $conversion): UrlGeneratorInterface
54
    {
55 1
        $this->conversion = $conversion;
56
57 1
        return $this;
58
    }
59 1
60
    public function setPathGenerator(AbstractPathGenerator $pathGenerator): UrlGeneratorInterface
61
    {
62
        $this->pathGenerator = $pathGenerator;
63
64
        return $this;
65
    }
66
67
    public function __toString(): string
68
    {
69
        return $this->getUrl();
70
    }
71
72
//    protected function getDiskName(): string
73
//    {
74
//        return $this->conversion === null
75
//            ? $this->media->disk
76
//            : $this->media->conversions_disk;
77
//    }
78
79
    protected function getDisk(): FileDisk
80
    {
81
//        return app('filesystem')->disk($this->getDiskName());
82
        return $this->media->getFile()->getFilesystem();
83
    }
84
}
85