Completed
Push — master ( f75f70...14f92d )
by Freek
01:32
created

BaseUrlGenerator::rawUrlEncodeFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary\UrlGenerator;
4
5
use Spatie\MediaLibrary\Media;
6
use Spatie\MediaLibrary\Conversion\Conversion;
7
use Spatie\MediaLibrary\PathGenerator\PathGenerator;
8
use Illuminate\Contracts\Config\Repository as Config;
9
10
abstract class BaseUrlGenerator implements UrlGenerator
11
{
12
    /** @var \Spatie\MediaLibrary\Media */
13
    protected $media;
14
15
    /** @var \Spatie\MediaLibrary\Conversion\Conversion */
16
    protected $conversion;
17
18
    /** @var \Spatie\MediaLibrary\PathGenerator\PathGenerator */
19
    protected $pathGenerator;
20
21
    /** @var \Illuminate\Contracts\Config\Repository */
22
    protected $config;
23
24
    /** @param \Illuminate\Contracts\Config\Repository $config */
25
    public function __construct(Config $config)
26
    {
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * @param \Spatie\MediaLibrary\Media $media
32
     *
33
     * @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator
34
     */
35
    public function setMedia(Media $media): UrlGenerator
36
    {
37
        $this->media = $media;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param \Spatie\MediaLibrary\Conversion\Conversion $conversion
44
     *
45
     * @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator
46
     */
47
    public function setConversion(Conversion $conversion): UrlGenerator
48
    {
49
        $this->conversion = $conversion;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param \Spatie\MediaLibrary\PathGenerator\PathGenerator $pathGenerator
56
     *
57
     * @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator
58
     */
59
    public function setPathGenerator(PathGenerator $pathGenerator): UrlGenerator
60
    {
61
        $this->pathGenerator = $pathGenerator;
62
63
        return $this;
64
    }
65
66
    /*
67
     * Get the path to the requested file relative to the root of the media directory.
68
     */
69
    public function getPathRelativeToRoot(): string
70
    {
71
        if (is_null($this->conversion)) {
72
            return $this->pathGenerator->getPath($this->media).($this->media->file_name);
73
        }
74
75
        return $this->pathGenerator->getPathForConversions($this->media)
76
        .$this->conversion->getName()
77
        .'.'
78
        .$this->conversion->getResultExtension($this->media->extension);
79
    }
80
81
    public function rawUrlEncodeFilename(string $path = ''): string
82
    {
83
        return pathinfo($path, PATHINFO_DIRNAME).'/'.rawurlencode(pathinfo($path, PATHINFO_BASENAME));
84
    }
85
}
86