LocalUrlGenerator::getBaseMediaDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary\UrlGenerator;
4
5
use DateTimeInterface;
6
use Illuminate\Support\Str;
7
use Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined;
8
9
class LocalUrlGenerator extends BaseUrlGenerator
10
{
11
    /**
12
     * Get the url for a media item.
13
     *
14
     * @return string
15
     *
16
     * @throws \Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined
17
     */
18
    public function getUrl(): string
19
    {
20
        $url = $this->getBaseMediaDirectoryUrl().'/'.$this->getPathRelativeToRoot();
21
22
        $url = $this->makeCompatibleForNonUnixHosts($url);
23
24
        $url = $this->rawUrlEncodeFilename($url);
25
26
        $url = $this->versionUrl($url);
27
28
        return $url;
29
    }
30
31
    /**
32
     * @param \DateTimeInterface $expiration
33
     * @param array              $options
34
     *
35
     * @return string
36
     *
37
     * @throws \Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined
38
     */
39
    public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string
40
    {
41
        throw UrlCannotBeDetermined::filesystemDoesNotSupportTemporaryUrls();
42
    }
43
44
    /*
45
     * Get the path for the profile of a media item.
46
     */
47
    public function getPath(): string
48
    {
49
        return $this->getStoragePath().'/'.$this->getPathRelativeToRoot();
50
    }
51
52
    protected function getBaseMediaDirectoryUrl(): string
53
    {
54
        if ($diskUrl = $this->config->get("filesystems.disks.{$this->media->disk}.url")) {
55
            return str_replace(url('/'), '', $diskUrl);
56
        }
57
58
        if (! Str::startsWith($this->getStoragePath(), public_path())) {
59
            throw UrlCannotBeDetermined::mediaNotPubliclyAvailable($this->getStoragePath(), public_path());
60
        }
61
62
        return $this->getBaseMediaDirectory();
63
    }
64
65
    /*
66
     * Get the directory where all files of the media item are stored.
67
     */
68
    protected function getBaseMediaDirectory(): string
69
    {
70
        return str_replace(public_path(), '', $this->getStoragePath());
71
    }
72
73
    /*
74
     * Get the path where the whole medialibrary is stored.
75
     */
76
    protected function getStoragePath(): string
77
    {
78
        $diskRootPath = $this->config->get("filesystems.disks.{$this->media->disk}.root");
79
80
        return realpath($diskRootPath);
81
    }
82
83
    protected function makeCompatibleForNonUnixHosts(string $url): string
84
    {
85
        if (DIRECTORY_SEPARATOR != '/') {
86
            $url = str_replace(DIRECTORY_SEPARATOR, '/', $url);
87
        }
88
89
        return $url;
90
    }
91
92
    /**
93
     * Get the url to the directory containing responsive images.
94
     *
95
     * @return string
96
     */
97
    public function getResponsiveImagesDirectoryUrl(): string
98
    {
99
        $base = Str::finish($this->getBaseMediaDirectoryUrl(), '/');
100
        $path = $this->pathGenerator->getPathForResponsiveImages($this->media);
101
102
        return Str::finish(url($base.$path), '/');
0 ignored issues
show
Bug introduced by
It seems like url($base . $path) targeting url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Illuminate\Support\Str::finish() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
    }
104
}
105