Passed
Push — master ( 9aef17...9e5266 )
by Gabriel
05:26
created

AbstractUrlGenerator::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 League\Flysystem\FilesystemInterface;
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
    public function getUrl(): string
27
    {
28
        if (!$this->media->hasFile()) {
29
            return $this->media->getCollection()->getDefaultMediaUrl();
30
        }
31
32
        return $this->getDisk()->getUrl($this->getPath());
33
    }
34
35
    public function getPath(): string
36
    {
37
        $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...
38
        return $this->media->getPath($convestion);
39
    }
40
41 1
    public function setMedia(Media $media): UrlGeneratorInterface
42
    {
43 1
        $this->media = $media;
44
45 1
        return $this;
46
    }
47
48
    public function setConversion(Conversion $conversion): UrlGeneratorInterface
49
    {
50
        $this->conversion = $conversion;
51
52
        return $this;
53
    }
54
55 1
    public function setPathGenerator(AbstractPathGenerator $pathGenerator): UrlGeneratorInterface
56
    {
57 1
        $this->pathGenerator = $pathGenerator;
0 ignored issues
show
Bug Best Practice introduced by
The property pathGenerator does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
59 1
        return $this;
60
    }
61
62
    public function __toString(): string
63
    {
64
        return $this->getUrl();
65
    }
66
67
//    protected function getDiskName(): string
68
//    {
69
//        return $this->conversion === null
70
//            ? $this->media->disk
71
//            : $this->media->conversions_disk;
72
//    }
73
74
    protected function getDisk(): FilesystemInterface
75
    {
76
//        return app('filesystem')->disk($this->getDiskName());
77
        return $this->media->getFile()->getFilesystem();
78
    }
79
}
80