Passed
Pull Request — master (#265)
by Pascal
02:28
created

Disk   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 37
c 6
b 0
f 0
dl 0
loc 146
rs 10
wmc 22

14 Methods

Rating   Name   Duplication   Size   Complexity  
A isLocalDisk() 0 3 1
A makeMedia() 0 3 1
A getFilesystemAdapter() 0 11 3
A getFlysystemDriver() 0 3 1
A getName() 0 7 2
A getFlysystemAdapter() 0 3 1
A make() 0 7 2
A getTemporaryDirectory() 0 7 2
A makeTemporaryDisk() 0 7 1
A clone() 0 3 1
A __construct() 0 3 1
A normalizePath() 0 12 3
A __call() 0 3 1
A path() 0 5 2
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
        return str_replace('\\', '/', $path);
133
134
        $normalizedPath = Util::normalizePath($path);
0 ignored issues
show
Unused Code introduced by
$normalizedPath = League...l::normalizePath($path) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
135
136
        // reapply the starting slash
137
        if (Str::startsWith($path, '/') && !Str::startsWith($normalizedPath, '/')) {
138
            return "/{$normalizedPath}";
139
        }
140
141
        return $normalizedPath;
142
    }
143
144
    /**
145
     * Get the full path for the file at the given "short" path.
146
     *
147
     * @param  string  $path
148
     * @return string
149
     */
150
    public function path(string $path): string
151
    {
152
        $path = $this->getFilesystemAdapter()->path($path);
153
154
        return $this->isLocalDisk() ? static::normalizePath($path) : $path;
155
    }
156
157
    /**
158
     * Forwards all calls to Laravel's FilesystemAdapter which will pass
159
     * dynamic methods call onto Flysystem.
160
     */
161
    public function __call($method, $parameters)
162
    {
163
        return $this->forwardCallTo($this->getFilesystemAdapter(), $method, $parameters);
164
    }
165
}
166