Test Failed
Pull Request — master (#31)
by Filippo
10:49 queued 07:49
created

Filesystem   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 158
rs 10
wmc 19

12 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 0 8 1
A path() 0 7 2
A makeDirectory() 0 4 1
A folder() 0 9 4
A __construct() 0 3 1
A list() 0 6 1
A folders() 0 5 1
A disk() 0 7 3
A isLocal() 0 8 1
A __call() 0 7 2
A filesystem() 0 3 1
A exists() 0 5 1
1
<?php
2
3
namespace Jobtech\LaravelChunky\Support;
4
5
use Illuminate\Container\Container;
0 ignored issues
show
Bug introduced by
The type Illuminate\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Contracts\Filesystem\Factory;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
use Illuminate\Support\Traits\ForwardsCalls;
10
use League\Flysystem\Adapter\Local;
11
12
abstract class Filesystem
13
{
14
    use ForwardsCalls;
15
16
    /** @var \Illuminate\Contracts\Filesystem\Factory */
17
    private Factory $filesystem;
18
19
    /** @var string|null */
20
    protected ?string $disk = null;
21
22
    /** @var string|null */
23
    protected ?string $folder = null;
24
25
    public function __construct(Factory $filesystem)
26
    {
27
        $this->filesystem = $filesystem;
28
    }
29
30
    /**
31
     * @param string $path
32
     *
33
     * @return string
34
     */
35
    protected function path(string $path): string
36
    {
37
        if (! Str::startsWith($path, $this->folder)) {
38
            return $this->folder().$path;
39
        }
40
41
        return $path;
42
    }
43
44
    /**
45
     * @return \Illuminate\Contracts\Filesystem\Factory
46
     *
47
     * @codeCoverageIgnore
48
     */
49
    public function filesystem(): Factory
50
    {
51
        return $this->filesystem;
52
    }
53
54
    /**
55
     * Disk getter and setter.
56
     *
57
     * @param string|null $disk
58
     * @return string|null
59
     */
60
    public function disk($disk = null): ?string
61
    {
62
        if (! empty($disk) && is_string($disk)) {
63
            $this->disk = $disk;
64
        }
65
66
        return $this->disk;
67
    }
68
69
    /**
70
     * Folder getter and setter.
71
     *
72
     * @param string|null $folder
73
     * @return string|null
74
     */
75
    public function folder($folder = null): ?string
76
    {
77
        if (! empty($folder) && is_string($folder)) {
78
            $suffix = Str::endsWith($folder, DIRECTORY_SEPARATOR) ? '' : DIRECTORY_SEPARATOR;
79
80
            $this->folder = $folder.$suffix;
81
        }
82
83
        return $this->folder;
84
    }
85
86
    /**
87
     * @return bool
88
     *
89
     * @codeCoverageIgnore
90
     */
91
    public function isLocal(): bool
92
    {
93
        $adapter = $this->filesystem()
94
            ->disk($this->disk)
95
            ->getDriver()
96
            ->getAdapter();
97
98
        return $adapter instanceof Local;
99
    }
100
101
    /**
102
     * @param string $path
103
     * @return bool
104
     */
105
    public function exists(string $path): bool
106
    {
107
        return $this->filesystem()
108
            ->disk($this->disk())
109
            ->exists($this->path($path));
110
    }
111
112
    /**
113
     * Retrieve every chunks' folder.
114
     *
115
     * @return array
116
     */
117
    public function folders(): array
118
    {
119
        return $this->filesystem()
120
            ->disk($this->disk())
121
            ->directories($this->folder());
122
    }
123
124
    /**
125
     * @param string|null $folder
126
     *
127
     * @return array
128
     */
129
    public function list($folder = ''): array
130
    {
131
        return $this->filesystem()->disk(
132
            $this->disk()
133
        )->files(
134
            $this->path($folder)
0 ignored issues
show
Bug introduced by
It seems like $folder can also be of type null; however, parameter $path of Jobtech\LaravelChunky\Support\Filesystem::path() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
            $this->path(/** @scrutinizer ignore-type */ $folder)
Loading history...
135
        );
136
    }
137
138
    /**
139
     * @param string $folder
140
     * @return bool
141
     */
142
    public function makeDirectory(string $folder): bool
143
    {
144
        return $this->filesystem->disk($this->disk)
145
            ->makeDirectory($this->path($folder));
146
    }
147
148
    /**
149
     * @param array $config
150
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
151
     * @return mixed
152
     */
153
    public static function instance(array $config)
154
    {
155
        $filesystem = Container::getInstance()->make(get_called_class());
156
157
        $filesystem->disk(Arr::get($config, 'disk'));
158
        $filesystem->folder(Arr::get($config, 'folder'));
159
160
        return $filesystem;
161
    }
162
163
    public function __call($method, $parameters)
164
    {
165
        if (! method_exists($this, $method)) {
166
            return $this->forwardCallTo($this->filesystem(), $method, $parameters);
167
        }
168
169
        return $this->{$method}($parameters);
170
    }
171
}
172