Passed
Push — master ( 7956e0...e8a176 )
by Gabriel
04:23
created

FilesystemManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 68.63%

Importance

Changes 5
Bugs 0 Features 4
Metric Value
eloc 47
c 5
b 0
f 4
dl 0
loc 177
rs 10
ccs 35
cts 51
cp 0.6863
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A adapt() 0 3 1
A callCustomCreator() 0 8 2
A createLocalDriver() 0 17 2
A __construct() 0 4 2
A resolve() 0 19 5
A getConfig() 0 14 4
A set() 0 3 1
A createDisk() 0 7 2
A getDefaultCloudDriver() 0 3 1
1
<?php
2
3
namespace Nip\Filesystem;
4
5
use InvalidArgumentException;
6
use League\Flysystem\Adapter\Local as LocalAdapter;
7
use League\Flysystem\AdapterInterface;
8
use League\Flysystem\FilesystemInterface;
9
use Nip\Config\Config;
10
11
/**
12
 * Class FilesystemManager
13
 * @package Nip\Filesystem
14
 */
15
class FilesystemManager
16
{
17
    use FilesystemManager\HasCloudDriverTrait;
18
    use FilesystemManager\HasDisksTrait;
19
    use FilesystemManager\HasCacheStoreTrait;
20
21
    /**
22
     * The application instance.
23
     *
24
     * @var \Nip\Application
0 ignored issues
show
Bug introduced by
The type Nip\Application 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...
25
     */
26
    protected $app;
27
28
    /**
29
     * The registered custom driver creators.
30
     *
31
     * @var array
32
     */
33
    protected $customCreators = [];
34
35
36
    /**
37
     * Create a new filesystem manager instance.
38
     *
39
     * @param  \Nip\Application $app
40
     */
41 4
    public function __construct($app = null)
42
    {
43 4
        if ($app) {
44
            $this->app = $app;
45
        }
46 4
    }
47
48
    /**
49
     * Resolve the given disk.
50
     *
51
     * @param  string $name
52
     * @return FileDisk
53
     *
54
     * @throws \InvalidArgumentException
55
     */
56 1
    protected function resolve($name)
57
    {
58 1
        $config = $this->getConfig($name);
59 1
        if (empty($config)) {
60
            throw new InvalidArgumentException("No configuration found for Disk [{$name}].");
61
        }
62
63 1
        if (is_string($config)) {
0 ignored issues
show
introduced by
The condition is_string($config) is always false.
Loading history...
64 1
            return $this->get($config);
65
        }
66
67 1
        if (isset($this->customCreators[$config['driver']])) {
68
            return $this->callCustomCreator($config);
69
        }
70 1
        $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
71 1
        if (method_exists($this, $driverMethod)) {
72 1
            return $this->{$driverMethod}($config);
73
        } else {
74
            throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
75
        }
76
    }
77
78
    /**
79
     * Get the filesystem connection configuration.
80
     *
81
     * @param  string $name
82
     * @return array
83
     */
84 1
    protected function getConfig($name)
85
    {
86 1
        if (!function_exists('config')) {
87
            return null;
88
        }
89 1
        $config = config();
90 1
        $configName = "filesystems.disks.{$name}";
91 1
        if (!$config->has($configName)) {
92
            return null;
93
        }
94
95 1
        $value = $config->get($configName);
96
97 1
        return $value instanceof Config ? $value->toArray() : $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value instanceof...lue->toArray() : $value also could return the type string which is incompatible with the documented return type array.
Loading history...
98
    }
99
100
    /**
101
     * Call a custom driver creator.
102
     *
103
     * @param  array $config
104
     * @return FileDisk
105
     */
106
    protected function callCustomCreator(array $config)
107
    {
108
        $driver = $this->customCreators[$config['driver']]($this->app, $config);
109
        if ($driver instanceof FilesystemInterface) {
110
            return $this->adapt($driver);
111
        }
112
113
        return $driver;
114
    }
115
116
    /**
117
     * Adapt the filesystem implementation.
118
     *
119
     * @param  \League\Flysystem\FilesystemInterface $filesystem
120
     * @return \League\Flysystem\FilesystemInterface|FileDisk
121
     */
122 3
    protected function adapt(FilesystemInterface $filesystem)
123
    {
124 3
        return $filesystem;
125
//        return new FlysystemAdapter($filesystem);
126
    }
127
128
    /**
129
     * Create an instance of the local driver.
130
     *
131
     * @param  array $config
132
     * @return \League\Flysystem\FilesystemInterface
133
     */
134 3
    public function createLocalDriver($config)
135
    {
136 3
        $permissions = isset($config['permissions']) ? $config['permissions'] : [];
137 3
        $links = [];
138
//        $links = Arr::get($config, 'links') === 'skip'
139
//            ? LocalAdapter::SKIP_LINKS
140
//            : LocalAdapter::DISALLOW_LINKS;
141
142 3
        return $this->adapt(
143 3
            $this->createDisk(
144 3
                new LocalAdapter(
145 3
                    $config['root'],
146 3
                    LOCK_EX,
147 3
                    $links,
0 ignored issues
show
Bug introduced by
$links of type array is incompatible with the type integer expected by parameter $linkHandling of League\Flysystem\Adapter\Local::__construct(). ( Ignorable by Annotation )

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

147
                    /** @scrutinizer ignore-type */ $links,
Loading history...
148 3
                    $permissions
149
                ),
150 3
                $config
151
            )
152
        );
153
    }
154
155
    /**
156
     * Create a Flysystem instance with the given adapter.
157
     *
158
     * @param  \League\Flysystem\AdapterInterface $adapter
159
     * @param  array $config
160
     * @return FileDisk
161
     */
162 3
    protected function createDisk(AdapterInterface $adapter, $config)
163
    {
164
//        $config = Arr::only($config, ['visibility', 'disable_asserts', 'url']);
165
166 3
        $this->checkForCacheNeeded($adapter, $config);
167
168 3
        return new FileDisk($adapter, count($config) > 0 ? $config : null);
169
    }
170
171
172
    /**
173
     * Set the given disk instance.
174
     *
175
     * @param  string $name
176
     * @param  FileDisk $disk
177
     * @return void
178
     */
179
    public function set($name, $disk)
180
    {
181
        $this->disks[$name] = $disk;
182
    }
183
184
    /**
185
     * Get the default cloud driver name.
186
     *
187
     * @return string
188
     */
189
    public function getDefaultCloudDriver()
190
    {
191
        return config('filesystems.cloud');
0 ignored issues
show
Bug Best Practice introduced by
The expression return config('filesystems.cloud') also could return the type Nip\Config\Config which is incompatible with the documented return type string.
Loading history...
192
    }
193
}
194