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

LocalUrlGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 3 Features 2
Metric Value
wmc 8
c 4
b 3
f 2
lcom 1
cbo 4
dl 0
loc 80
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 10 2
A getFullUrl() 0 4 1
A getPath() 0 4 1
A getBaseMediaDirectory() 0 6 1
A getStoragePath() 0 6 1
A makeCompatibleForNonUnixHosts() 0 8 2
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