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

LocalUrlGenerator::getFullUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary\UrlGenerator;
4
5
use Spatie\MediaLibrary\Exceptions\UrlCouldNotBeDetermined;
6
7
class LocalUrlGenerator extends BaseUrlGenerator implements UrlGenerator
8
{
9
    /**
10
     * Get the url for the profile of a media item.
11
     *
12
     * @return string
13
     *
14
     * @throws \Spatie\MediaLibrary\Exceptions\UrlCouldNotBeDetermined
15
     */
16
    public function getUrl()
17
    {
18
        if (!string($this->getStoragePath())->startsWith(public_path())) {
19
            throw new UrlCouldNotBeDetermined('The storage path is not part of the public path');
20
        }
21
22
        $url = $this->getBaseMediaDirectory().'/'.$this->getPathRelativeToRoot();
23
24
        return $this->makeCompatibleForNonUnixHosts($url);
25
    }
26
27
    /**
28
     * Get the full url for the profile of a media item.
29
     *
30
     * @return string
31
     *
32
     * @throws \Spatie\MediaLibrary\Exceptions\UrlCouldNotBeDetermined
33
     */
34
    public function getFullUrl()
35
    {
36
        return url($this->getUrl());
0 ignored issues
show
Bug Compatibility introduced by
The expression url($this->getUrl()); of type Illuminate\Contracts\Routing\UrlGenerator|string adds the type Illuminate\Contracts\Routing\UrlGenerator to the return on line 36 which is incompatible with the return type declared by the interface Spatie\MediaLibrary\UrlG...rlGenerator::getFullUrl of type string.
Loading history...
37
    }
38
39
    /**
40
     * Get the path for the profile of a media item.
41
     *
42
     * @return string
43
     */
44
    public function getPath()
45
    {
46
        return $this->getStoragePath().'/'.$this->getPathRelativeToRoot();
47
    }
48
49
    /**
50
     * Get the directory where all files of the media item are stored.
51
     *
52
     * @return \Spatie\String\Str
53
     */
54
    protected function getBaseMediaDirectory()
55
    {
56
        $baseDirectory = string($this->getStoragePath())->replace(public_path(), '');
57
58
        return $baseDirectory;
59
    }
60
61
    /**
62
     * Get the path where the whole medialibrary is stored.
63
     *
64
     * @return string
65
     */
66
    protected function getStoragePath()
67
    {
68
        $diskRootPath = $this->config->get('filesystems.disks.'.$this->media->disk.'.root');
69
70
        return realpath($diskRootPath);
71
    }
72
73
    /**
74
     * @param string $url
75
     *
76
     * @return string
77
     */
78
    protected function makeCompatibleForNonUnixHosts($url)
79
    {
80
        if (DIRECTORY_SEPARATOR != '/') {
81
            $url = str_replace(DIRECTORY_SEPARATOR, '/', $url);
82
        }
83
84
        return $url;
85
    }
86
}
87