MountManager::addPlugin()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace League\Flysystem;
4
5
use InvalidArgumentException;
6
use League\Flysystem\Plugin\PluginNotFoundException;
7
use LogicException;
8
use BadMethodCallException;
9
use League\Flysystem\FilesystemInterface;
10
use League\Flysystem\PluginInterface;
11
12
/**
13
 * Class MountManager.
14
 *
15
 * Proxies methods to Filesystem (@see __call):
16
 *
17
 * @method AdapterInterface getAdapter($prefix)
18
 * @method Config getConfig($prefix)
19
 * @method bool has($path)
20
 * @method bool write($path, $contents, array $config = [])
21
 * @method bool writeStream($path, $resource, array $config = [])
22
 * @method bool put($path, $contents, $config = [])
23
 * @method bool putStream($path, $contents, $config = [])
24
 * @method string readAndDelete($path)
25
 * @method bool update($path, $contents, $config = [])
26
 * @method bool updateStream($path, $resource, $config = [])
27
 * @method string|false read($path)
28
 * @method resource|false readStream($path)
29
 * @method bool rename($path, $newpath)
30
 * @method bool delete($path)
31
 * @method bool deleteDir($dirname)
32
 * @method bool createDir($dirname, $config = [])
33
 * @method array listFiles($directory = '', $recursive = false)
34
 * @method array listPaths($directory = '', $recursive = false)
35
 * @method array getWithMetadata($path, array $metadata)
36
 * @method string|false getMimetype($path)
37
 * @method string|false getTimestamp($path)
38
 * @method string|false getVisibility($path)
39
 * @method int|false getSize($path);
40
 * @method bool setVisibility($path, $visibility)
41
 * @method array|false getMetadata($path)
42
 * @method Handler get($path, Handler $handler = null)
43
 * @method Filesystem flushCache()
44
 * @method assertPresent($path)
45
 * @method assertAbsent($path)
46
 * @method Filesystem addPlugin(PluginInterface $plugin)
47
 */
48
class MountManager
49
{
50
    /**
51
     * @var array
52
     */
53
    protected $plugins = array();
54
55
    /**
56
     * Register a plugin.
57
     *
58
     * @param PluginInterface $plugin
59
     *
60
     * @return $this
61
     */
62
    public function addPlugin(PluginInterface $plugin)
63
    {
64
        $this->plugins[$plugin->getMethod()] = $plugin;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Find a specific plugin.
71
     *
72
     * @param string $method
73
     *
74
     * @throws LogicException
75
     *
76
     * @return PluginInterface $plugin
77
     */
78 18 View Code Duplication
    protected function findPlugin($method)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80 18
        if ( ! isset($this->plugins[$method])) {
81 18
            throw new PluginNotFoundException('Plugin not found for method: ' . $method);
82
        }
83
84
        if ( ! method_exists($this->plugins[$method], 'handle')) {
85
            throw new LogicException(get_class($this->plugins[$method]) . ' does not have a handle method.');
86
        }
87
88
        return $this->plugins[$method];
89
    }
90
91
    /**
92
     * Invoke a plugin by method name.
93
     *
94
     * @param string $method
95
     * @param array  $arguments
96
     *
97
     * @return mixed
98
     */
99 18 View Code Duplication
    protected function invokePlugin($method, array $arguments, FilesystemInterface $filesystem)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101 18
        $plugin = $this->findPlugin($method);
102
        $plugin->setFilesystem($filesystem);
103
        $callback = array($plugin, 'handle');
104
105
        return call_user_func_array($callback, $arguments);
106
    }
107
108
109
110
    /**
111
     * @var array
112
     */
113
    protected $filesystems = array();
114
115
    /**
116
     * Constructor.
117
     *
118
     * @param array $filesystems
119
     */
120 45
    public function __construct(array $filesystems = array())
121
    {
122 45
        $this->mountFilesystems($filesystems);
123 45
    }
124
125
    /**
126
     * Mount filesystems.
127
     *
128
     * @param array $filesystems [:prefix => Filesystem,]
129
     *
130
     * @return $this
131
     */
132 45
    public function mountFilesystems(array $filesystems)
133
    {
134 45
        foreach ($filesystems as $prefix => $filesystem) {
135 3
            $this->mountFilesystem($prefix, $filesystem);
136 45
        }
137
138 45
        return $this;
139
    }
140
141
    /**
142
     * Mount filesystems.
143
     *
144
     * @param string              $prefix
145
     * @param FilesystemInterface $filesystem
146
     *
147
     * @return $this
148
     */
149 33
    public function mountFilesystem($prefix, FilesystemInterface $filesystem)
150
    {
151 33
        if ( ! is_string($prefix)) {
152 3
            throw new InvalidArgumentException(__METHOD__ . ' expects argument #1 to be a string.');
153
        }
154
155 30
        $this->filesystems[$prefix] = $filesystem;
156
157 30
        return $this;
158
    }
159
160
    /**
161
     * Get the filesystem with the corresponding prefix.
162
     *
163
     * @param string $prefix
164
     *
165
     * @throws LogicException
166
     *
167
     * @return FilesystemInterface
168
     */
169 33
    public function getFilesystem($prefix)
170
    {
171 33
        if ( ! isset($this->filesystems[$prefix])) {
172 3
            throw new LogicException('No filesystem mounted with prefix ' . $prefix);
173
        }
174
175 30
        return $this->filesystems[$prefix];
176
    }
177
178
    /**
179
     * Retrieve the prefix from an arguments array.
180
     *
181
     * @param array $arguments
182
     *
183
     * @return array [:prefix, :arguments]
184
     */
185 36
    public function filterPrefix(array $arguments)
186
    {
187 36
        if (empty($arguments)) {
188 3
            throw new LogicException('At least one argument needed');
189
        }
190
191 33
        $path = array_shift($arguments);
192
193 33
        if ( ! is_string($path)) {
194 3
            throw new InvalidArgumentException('First argument should be a string');
195
        }
196
197 30
        if ( ! preg_match('#^.+\:\/\/.*#', $path)) {
198 3
            throw new InvalidArgumentException('No prefix detected in path: ' . $path);
199
        }
200
201 27
        list($prefix, $path) = explode('://', $path, 2);
202 27
        array_unshift($arguments, $path);
203
204 27
        return array($prefix, $arguments);
205
    }
206
207
    /**
208
     * @param string $directory
209
     * @param bool   $recursive
210
     *
211
     * @return array
212
     */
213 3
    public function listContents($directory = '', $recursive = false)
214
    {
215 3
        list($prefix, $arguments) = $this->filterPrefix(array($directory));
216 3
        $filesystem = $this->getFilesystem($prefix);
217 3
        $directory = array_shift($arguments);
218 3
        $result = $filesystem->listContents($directory, $recursive);
219
220 3
        foreach ($result as &$file) {
221 3
            $file['filesystem'] = $prefix;
222 3
        }
223
224 3
        return $result;
225
    }
226
227
    /**
228
     * Call forwarder.
229
     *
230
     * @param string $method
231
     * @param array  $arguments
232
     *
233
     * @return mixed
234
     */
235 15
    public function __call($method, $arguments)
236
    {
237 15
        list($prefix, $arguments) = $this->filterPrefix($arguments);
238
239 15
        return $this->invokePluginOnFilesystem($method, $arguments, $prefix);
240
    }
241
242
    /**
243
     * @param $from
244
     * @param $to
245
     * @param array $config
246
     *
247
     * @return bool
248
     */
249 6
    public function copy($from, $to, array $config = array())
250
    {
251 6
        list($prefixFrom, $arguments) = $this->filterPrefix(array($from));
252
253 6
        $fsFrom = $this->getFilesystem($prefixFrom);
254 6
        $buffer = call_user_func_array(array($fsFrom, 'readStream'), $arguments);
255
256 6
        if ($buffer === false) {
257 3
            return false;
258
        }
259
260 6
        list($prefixTo, $arguments) = $this->filterPrefix(array($to));
261
262 6
        $fsTo = $this->getFilesystem($prefixTo);
263 6
        $result =  call_user_func_array(array($fsTo, 'writeStream'), array_merge($arguments, array($buffer, $config)));
264
265 6
        if (is_resource($buffer)) {
266 6
            fclose($buffer);
267 6
        }
268
269 6
        return $result;
270
    }
271
272
    /**
273
     * @param array $keys
274
     * @param string $directory
275
     * @param bool $recursive
276
     * @return mixed
277
     */
278 3
    public function listWith(array $keys = array(), $directory = '', $recursive = false)
279
    {
280 3
        list($prefix, $arguments) = $this->filterPrefix(array($directory));
281 3
        $directory = $arguments[0];
282 3
        $arguments = array($keys, $directory, $recursive);
283
284 3
        return $this->invokePluginOnFilesystem('listWith', $arguments, $prefix);
285
    }
286
287
    /**
288
     * Move a file.
289
     *
290
     * @param $from
291
     * @param $to
292
     * @param array $config
293
     *
294
     * @return bool
295
     */
296 3
    public function move($from, $to, array $config = array())
297
    {
298 3
        $copied = $this->copy($from, $to, $config);
299
300 3
        if ($copied) {
301 3
            return $this->delete($from);
302
        }
303
304 3
        return false;
305
    }
306
307
    /**
308
     * Invoke a plugin on a filesystem mounted on a given prefix.
309
     *
310
     * @param $method
311
     * @param $arguments
312
     * @param $prefix
313
     *
314
     * @return mixed
315
     */
316 18
    public function invokePluginOnFilesystem($method, $arguments, $prefix)
317
    {
318 18
        $filesystem = $this->getFilesystem($prefix);
319
320
        try {
321 18
            return $this->invokePlugin($method, $arguments, $filesystem);
322 18
        } catch (PluginNotFoundException $e) {
323
            // Let it pass, it's ok, don't panic.
324
        }
325
326 18
        $callback = array($filesystem, $method);
327
328 18
        return call_user_func_array($callback, $arguments);
329
    }
330
}
331