Completed
Pull Request — master (#198)
by
unknown
02:16
created

BaseUrlGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary\UrlGenerator;
4
5
use Illuminate\Contracts\Config\Repository as Config;
6
use Spatie\MediaLibrary\Conversion\Conversion;
7
use Spatie\MediaLibrary\PathGenerator\PathGenerator;
8
9
abstract class BaseUrlGenerator
10
{
11
    /**
12
     * @var \Spatie\MediaLibrary\Media
13
     */
14
    protected $media;
15
16
    /**
17
     * @var \Spatie\MediaLibrary\Conversion\Conversion
18
     */
19
    protected $conversion;
20
21
    /**
22
     * @var \Spatie\MediaLibrary\PathGenerator\PathGenerator
23
     */
24
    protected $pathGenerator;
25
26
    /**
27
     * @var \Illuminate\Contracts\Config\Repository
28
     */
29
    protected $config;
30
31
    /**
32
     * @param \Illuminate\Contracts\Config\Repository $config
33
     */
34
    public function __construct(Config $config)
35
    {
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * @param \Spatie\MediaLibrary\Media $media
41
     *
42
     * @return $this
43
     */
44
    public function setMedia($media)
45
    {
46
        $this->media = $media;
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param \Spatie\MediaLibrary\Conversion\Conversion $conversion
53
     *
54
     * @return $this
55
     */
56
    public function setConversion(Conversion $conversion)
57
    {
58
        $this->conversion = $conversion;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param \Spatie\MediaLibrary\PathGenerator\PathGenerator $pathGenerator
65
     *
66
     * @return $this
67
     */
68
    public function setPathGenerator(PathGenerator $pathGenerator)
69
    {
70
        $this->pathGenerator = $pathGenerator;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Get the full url for the profile of a media item.
77
     *
78
     * @return string
79
     *
80
     * @throws \Spatie\MediaLibrary\Exceptions\UrlCouldNotBeDetermined
81
     */
82
    public function getFullUrl()
83
    {
84
        return $this->getUrl();
85
    }
86
87
    /**
88
     * Get the path to the requested file relative to the root of the media directory.
89
     *
90
     * @return string
91
     */
92
    public function getPathRelativeToRoot()
93
    {
94
        if (is_null($this->conversion)) {
95
            return $this->pathGenerator->getPath($this->media).$this->media->file_name;
96
        }
97
98
        return $this->pathGenerator->getPathForConversions($this->media).
99
            $this->conversion->getName().'.'.$this->conversion->getResultExtension($this->media->extension);
100
    }
101
}
102