Completed
Pull Request — master (#42)
by Florent
12:58
created

FilesystemFactory::__invoke()   D

Complexity

Conditions 16
Paths 209

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 0
cts 30
cp 0
rs 4.6208
c 0
b 0
f 0
cc 16
nc 209
nop 3
crap 272

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 League\Flysystem\Cached\CachedAdapter;
11
use League\Flysystem\Cached\CacheInterface;
12
use League\Flysystem\EventableFilesystem\EventableFilesystem;
13
use League\Flysystem\Filesystem;
14
use League\Flysystem\FilesystemInterface;
15
use Psr\Container\ContainerInterface;
16
use Zend\Cache\Storage\StorageInterface;
17
18
class FilesystemFactory
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $options;
24
25 1
    public function __construct(array $options = [])
26
    {
27 1
        $this->setCreationOptions($options);
28 1
    }
29
30 1
    public function setCreationOptions(array $options)
31
    {
32 1
        $this->options = $options;
33
34 1
        if (! isset($this->options['adapter_options'])) {
35 1
            $this->options['adapter_options'] = [];
36
        }
37 1
    }
38
39 View Code Duplication
    public function createService(ContainerInterface $container): FilesystemInterface
40
    {
41
        if (method_exists($container, 'getServiceLocator')) {
42
            $serviceLocator = $container->getServiceLocator();
0 ignored issues
show
Unused Code introduced by
$serviceLocator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
        }
44
45
        $requestedName = func_get_arg(2);
46
47
        return $this($container, $requestedName);
48
    }
49
50
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): FilesystemInterface
51
    {
52
        $config = $container->get('config');
53
        $fsConfig = $config['bsb_flysystem']['filesystems'][$requestedName];
54
        if (! isset($fsConfig['adapter'])) {
55
            throw new UnexpectedValueException(sprintf(
56
                "Missing 'adapter' key for the filesystem '%s' configuration",
57
                $requestedName
58
            ));
59
        }
60
61
        if (null !== $options) {
62
            $this->setCreationOptions($options);
63
        }
64
65
        $adapter = $container
66
            ->get('BsbFlysystemAdapterManager')
67
            ->get($fsConfig['adapter'], $this->options['adapter_options']);
68
69
        $options = isset($fsConfig['options']) && is_array($fsConfig['options']) ? $fsConfig['options'] : [];
70
71
        if (isset($fsConfig['cache']) && is_string($fsConfig['cache'])) {
72
            if (! class_exists(CachedAdapter::class)) {
73
                throw new RequirementsException(['league/flysystem-cached-adapter'], 'CachedAdapter');
74
            }
75
76
            $cacheAdapter = $container->get($fsConfig['cache']);
77
78
            // wrap if StorageInterface, use filesystem name a key
79
            if ($cacheAdapter instanceof StorageInterface) {
80
                $cacheAdapter = new ZendStorageCache($cacheAdapter, $requestedName);
81
            }
82
83
            // ignore if not CacheInterface
84
            if ($cacheAdapter instanceof CacheInterface) {
85
                $adapter = new CachedAdapter($adapter, $cacheAdapter);
86
            }
87
        }
88
89
        if (isset($fsConfig['eventable']) && filter_var($fsConfig['eventable'], FILTER_VALIDATE_BOOLEAN)) {
90
            if (! class_exists(EventableFilesystem::class)) {
91
                throw new RequirementsException(['league/flysystem-eventable-filesystem'], 'EventableFilesystem');
92
            }
93
94
            $filesystem = new EventableFilesystem($adapter, $options);
95
        } else {
96
            $filesystem = new Filesystem($adapter, $options);
97
        }
98
99
        if (isset($fsConfig['plugins']) && is_array($fsConfig['plugins'])) {
100
            foreach ($fsConfig['plugins'] as $plugin) {
101
                $plugin = new $plugin();
102
                $filesystem->addPlugin($plugin);
103
            }
104
        }
105
106
        return $filesystem;
107
    }
108
}
109