Passed
Push — master ( 5ccd86...1b90b9 )
by Gabriel
03:29
created

FilesystemManager   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 39.58%

Importance

Changes 5
Bugs 0 Features 4
Metric Value
wmc 18
eloc 44
c 5
b 0
f 4
dl 0
loc 171
rs 10
ccs 19
cts 48
cp 0.3958

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

140
                    /** @scrutinizer ignore-type */ $links,
Loading history...
141 2
                    $permissions
142
                ),
143 2
                $config
144
            )
145
        );
146
    }
147
148
    /**
149
     * Create a Flysystem instance with the given adapter.
150
     *
151
     * @param  \League\Flysystem\AdapterInterface $adapter
152
     * @param  array $config
153
     * @return FileDisk
154
     */
155 2
    protected function createDisk(AdapterInterface $adapter, $config)
156
    {
157
//        $config = Arr::only($config, ['visibility', 'disable_asserts', 'url']);
158
159 2
        $this->checkForCacheNeeded($adapter, $config);
160
161 2
        return new FileDisk($adapter, count($config) > 0 ? $config : null);
162
    }
163
164
165
    /**
166
     * Set the given disk instance.
167
     *
168
     * @param  string $name
169
     * @param  FileDisk $disk
170
     * @return void
171
     */
172
    public function set($name, $disk)
173
    {
174
        $this->disks[$name] = $disk;
175
    }
176
177
    /**
178
     * Get the default cloud driver name.
179
     *
180
     * @return string
181
     */
182
    public function getDefaultCloudDriver()
183
    {
184
        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...
185
    }
186
}
187