AbstractUrlGenerator::setPathGenerator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 2
rs 10
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