Completed
Push — master ( 455325...7625db )
by Bas
06:55 queued 02:45
created

FilesystemFactory::__invoke()   C

Complexity

Conditions 16
Paths 209

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 16.283

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 58
ccs 26
cts 29
cp 0.8966
rs 5.6402
cc 16
eloc 32
nc 209
nop 3
crap 16.283

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace BsbFlysystem\Filesystem\Factory;
6
7
use BsbFlysystem\Cache\ZendStorageCache;
8
use BsbFlysystem\Exception\RequirementsException;
9
use BsbFlysystem\Exception\UnexpectedValueException;
10
use Interop\Container\ContainerInterface;
11
use League\Flysystem\Cached\CachedAdapter;
12
use League\Flysystem\Cached\CacheInterface;
13
use League\Flysystem\EventableFilesystem\EventableFilesystem;
14
use League\Flysystem\Filesystem;
15
use League\Flysystem\FilesystemInterface;
16
use Zend\Cache\Storage\StorageInterface;
17
use Zend\ServiceManager\FactoryInterface;
18
use Zend\ServiceManager\ServiceLocatorInterface;
19
20
class FilesystemFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\FactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\FactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $options;
26
27 9
    public function __construct(array $options = [])
28
    {
29 9
        $this->setCreationOptions($options);
30 9
    }
31
32 9
    public function setCreationOptions(array $options)
33
    {
34 9
        $this->options = $options;
35
36 9
        if (! isset($this->options['adapter_options'])) {
37 9
            $this->options['adapter_options'] = [];
38
        }
39 9
    }
40
41 3 View Code Duplication
    public function createService(ServiceLocatorInterface $serviceLocator): FilesystemInterface
42
    {
43 3
        if (method_exists($serviceLocator, 'getServiceLocator')) {
44 3
            $serviceLocator = $serviceLocator->getServiceLocator();
45
        }
46
47 3
        $requestedName = func_get_arg(2);
48
49 3
        return $this($serviceLocator, $requestedName);
50
    }
51
52 9
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): FilesystemInterface
53
    {
54 9
        $config         = $container->get('config');
55 9
        $fsConfig       = $config['bsb_flysystem']['filesystems'][$requestedName];
56 9
        if (! isset($fsConfig['adapter'])) {
57 1
            throw new UnexpectedValueException(sprintf(
58 1
                "Missing 'adapter' key for the filesystem '%s' configuration",
59
                $requestedName
60
            ));
61
        }
62
63 8
        if (null !== $options) {
64
            $this->setCreationOptions($options);
65
        }
66
67
        $adapter = $container
68 8
            ->get('BsbFlysystemAdapterManager')
69 8
            ->get($fsConfig['adapter'], $this->options['adapter_options']);
70
71 8
        $options = isset($fsConfig['options']) && is_array($fsConfig['options']) ? $fsConfig['options'] : [];
72
73 8
        if (isset($fsConfig['cache']) && is_string($fsConfig['cache'])) {
74 3
            if (! class_exists(\League\Flysystem\Cached\CachedAdapter::class)) {
75
                throw new RequirementsException(['league/flysystem-cached-adapter'], 'CachedAdapter');
76
            }
77
78 3
            $cacheAdapter = $container->get($fsConfig['cache']);
79
80
            // wrap if StorageInterface, use filesystem name a key
81 3
            if ($cacheAdapter instanceof StorageInterface) {
82 2
                $cacheAdapter = new ZendStorageCache($cacheAdapter, $requestedName);
83
            }
84
85
            // ignore if not CacheInterface
86 3
            if ($cacheAdapter instanceof CacheInterface) {
87 3
                $adapter = new CachedAdapter($adapter, $cacheAdapter);
88
            }
89
        }
90
91 8
        if (isset($fsConfig['eventable']) && filter_var($fsConfig['eventable'], FILTER_VALIDATE_BOOLEAN)) {
92 1
            if (! class_exists(\League\Flysystem\EventableFilesystem\EventableFilesystem::class)) {
93
                throw new RequirementsException(['league/flysystem-eventable-filesystem'], 'EventableFilesystem');
94
            }
95
96 1
            $filesystem = new EventableFilesystem($adapter, $options);
97
        } else {
98 7
            $filesystem = new Filesystem($adapter, $options);
99
        }
100
101 8
        if (isset($fsConfig['plugins']) && is_array($fsConfig['plugins'])) {
102 1
            foreach ($fsConfig['plugins'] as $plugin) {
103 1
                $plugin = new $plugin();
104 1
                $filesystem->addPlugin($plugin);
105
            }
106
        }
107
108 8
        return $filesystem;
109
    }
110
}
111