Passed
Pull Request — master (#265)
by Pascal
05:56
created

Disk::normalizePath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Filesystem;
4
5
use Illuminate\Contracts\Filesystem\Filesystem;
6
use Illuminate\Filesystem\FilesystemAdapter;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Support\Str;
9
use Illuminate\Support\Traits\ForwardsCalls;
10
use League\Flysystem\Adapter\Local;
11
use League\Flysystem\AdapterInterface;
12
use League\Flysystem\Filesystem as LeagueFilesystem;
13
use League\Flysystem\Util;
14
15
/**
16
 * @mixin \Illuminate\Filesystem\FilesystemAdapter
17
 */
18
class Disk
19
{
20
    use ForwardsCalls;
21
22
    /**
23
     * @var string|\Illuminate\Contracts\Filesystem\Filesystem
24
     */
25
    private $disk;
26
27
    /**
28
     * @var string
29
     */
30
    private $temporaryDirectory;
31
32
    /**
33
     * @var \Illuminate\Filesystem\FilesystemAdapter
34
     */
35
    private $filesystemAdapter;
36
37
    public function __construct($disk)
38
    {
39
        $this->disk = $disk;
40
    }
41
42
    /**
43
     * Little helper method to instantiate this class.
44
     */
45
    public static function make($disk): self
46
    {
47
        if ($disk instanceof self) {
48
            return $disk;
49
        }
50
51
        return new static($disk);
52
    }
53
54
    public static function makeTemporaryDisk(): self
55
    {
56
        $filesystemAdapter = app('filesystem')->createLocalDriver([
57
            'root' => app(TemporaryDirectories::class)->create(),
58
        ]);
59
60
        return new static($filesystemAdapter);
61
    }
62
63
    /**
64
     * Creates a fresh instance, mostly used to force a new TemporaryDirectory.
65
     */
66
    public function clone(): self
67
    {
68
        return new Disk($this->disk);
69
    }
70
71
    /**
72
     * Creates a new TemporaryDirectory instance if none is set, otherwise
73
     * it returns the current one.
74
     */
75
    public function getTemporaryDirectory(): string
76
    {
77
        if ($this->temporaryDirectory) {
78
            return $this->temporaryDirectory;
79
        }
80
81
        return $this->temporaryDirectory = app(TemporaryDirectories::class)->create();
82
    }
83
84
    public function makeMedia(string $path): Media
85
    {
86
        return Media::make($this, $path);
87
    }
88
89
    /**
90
     * Returns the name of the disk. It generates a name if the disk
91
     * is an instance of Flysystem.
92
     */
93
    public function getName(): string
94
    {
95
        if (is_string($this->disk)) {
96
            return $this->disk;
97
        }
98
99
        return get_class($this->getFlysystemAdapter()) . "_" . md5(json_encode(serialize($this->getFlysystemAdapter())));
100
    }
101
102
    public function getFilesystemAdapter(): FilesystemAdapter
103
    {
104
        if ($this->filesystemAdapter) {
105
            return $this->filesystemAdapter;
106
        }
107
108
        if ($this->disk instanceof Filesystem) {
109
            return $this->filesystemAdapter = $this->disk;
0 ignored issues
show
Documentation Bug introduced by
$this->disk is of type Illuminate\Contracts\Filesystem\Filesystem, but the property $filesystemAdapter was declared to be of type Illuminate\Filesystem\FilesystemAdapter. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
110
        }
111
112
        return $this->filesystemAdapter = Storage::disk($this->disk);
113
    }
114
115
    private function getFlysystemDriver(): LeagueFilesystem
116
    {
117
        return $this->getFilesystemAdapter()->getDriver();
118
    }
119
120
    private function getFlysystemAdapter(): AdapterInterface
121
    {
122
        return $this->getFlysystemDriver()->getAdapter();
123
    }
124
125
    public function isLocalDisk(): bool
126
    {
127
        return $this->getFlysystemAdapter() instanceof Local;
128
    }
129
130
    public static function normalizePath($path): string
131
    {
132
        $normalizedPath = Util::normalizePath($path);
133
134
        // reapply the starting slash
135
        if (Str::startsWith($path, '/') && !Str::startsWith($normalizedPath, '/')) {
136
            return "/{$normalizedPath}";
137
        }
138
139
        return $normalizedPath;
140
    }
141
142
    /**
143
     * Get the full path for the file at the given "short" path.
144
     *
145
     * @param  string  $path
146
     * @return string
147
     */
148
    public function path(string $path): string
149
    {
150
        $path = $this->getFilesystemAdapter()->path($path);
151
152
        return $this->isLocalDisk() ? static::normalizePath($path) : $path;
153
    }
154
155
    /**
156
     * Forwards all calls to Laravel's FilesystemAdapter which will pass
157
     * dynamic methods call onto Flysystem.
158
     */
159
    public function __call($method, $parameters)
160
    {
161
        return $this->forwardCallTo($this->getFilesystemAdapter(), $method, $parameters);
162
    }
163
}
164