FilesystemFactory   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 88
Duplicated Lines 11.36 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 82.05%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 10
loc 88
ccs 32
cts 39
cp 0.8205
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCreationOptions() 0 8 2
A createService() 10 10 2
C __invoke() 0 55 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * BsbFlystem
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @see       https://bushbaby.nl/
10
 *
11
 * @copyright Copyright (c) 2014-2021 bushbaby multimedia. (https://bushbaby.nl)
12
 * @author    Bas Kamer <[email protected]>
13
 * @license   MIT
14
 *
15
 * @package   bushbaby/flysystem
16
 */
17
18
declare(strict_types=1);
19
20
namespace BsbFlysystem\Filesystem\Factory;
21
22
use BsbFlysystem\Cache\ZendStorageCache;
23
use BsbFlysystem\Exception\RequirementsException;
24
use BsbFlysystem\Exception\UnexpectedValueException;
25
use Laminas\Cache\Storage\StorageInterface;
26
use League\Flysystem\Cached\CachedAdapter;
27 9
use League\Flysystem\Cached\CacheInterface;
28
use League\Flysystem\EventableFilesystem\EventableFilesystem;
29 9
use League\Flysystem\Filesystem;
30 9
use League\Flysystem\FilesystemInterface;
31
use Psr\Container\ContainerInterface;
32 9
33
class FilesystemFactory
34 9
{
35
    /**
36 9
     * @var array
37 9
     */
38
    protected $options;
39 9
40
    public function __construct(array $options = [])
41
    {
42
        $this->setCreationOptions($options);
43
    }
44
45
    public function setCreationOptions(array $options): void
46
    {
47
        $this->options = $options;
48
49
        if (! isset($this->options['adapter_options'])) {
50
            $this->options['adapter_options'] = [];
51
        }
52 9
    }
53
54 9 View Code Duplication
    public function createService(ContainerInterface $container): FilesystemInterface
55 9
    {
56 9
        if (\method_exists($container, 'getServiceLocator')) {
57 1
            $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...
58 1
        }
59 1
60
        $requestedName = \func_get_arg(2);
61
62
        return $this($container, $requestedName);
63 8
    }
64 1
65
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): FilesystemInterface
66
    {
67
        $config = $container->get('config');
68 8
        $fsConfig = $config['bsb_flysystem']['filesystems'][$requestedName];
69 8
        if (! isset($fsConfig['adapter'])) {
70
            throw new UnexpectedValueException(\sprintf("Missing 'adapter' key for the filesystem '%s' configuration", $requestedName));
71 8
        }
72
73 8
        if (null !== $options) {
74 3
            $this->setCreationOptions($options);
75
        }
76
77
        $adapter = $container
78 3
            ->get('BsbFlysystemAdapterManager')
79
            ->get($fsConfig['adapter'], $this->options['adapter_options']);
80
81 3
        $options = $fsConfig['options'] ?? [];
82 2
83
        if (isset($fsConfig['cache']) && \is_string($fsConfig['cache'])) {
84
            if (! \class_exists(CachedAdapter::class)) {
85
                throw new RequirementsException(['league/flysystem-cached-adapter'], 'CachedAdapter');
86 3
            }
87 3
88
            $cacheAdapter = $container->get($fsConfig['cache']);
89
90
            // wrap if StorageInterface, use filesystem name a key
91 8
            if ($cacheAdapter instanceof StorageInterface) {
92 1
                $cacheAdapter = new ZendStorageCache($cacheAdapter, $requestedName);
93
            }
94
95
            // ignore if not CacheInterface
96 1
            if ($cacheAdapter instanceof CacheInterface) {
97
                $adapter = new CachedAdapter($adapter, $cacheAdapter);
98 7
            }
99
        }
100
101 8
        if (isset($fsConfig['eventable']) && \filter_var($fsConfig['eventable'], FILTER_VALIDATE_BOOLEAN)) {
102 1
            if (! \class_exists(EventableFilesystem::class)) {
103 1
                throw new RequirementsException(['league/flysystem-eventable-filesystem'], 'EventableFilesystem');
104 1
            }
105
106
            $filesystem = new EventableFilesystem($adapter, $options);
107
        } else {
108 8
            $filesystem = new Filesystem($adapter, $options);
109
        }
110
111
        if (isset($fsConfig['plugins']) && \is_array($fsConfig['plugins'])) {
112
            foreach ($fsConfig['plugins'] as $plugin) {
113
                $plugin = new $plugin();
114
                $filesystem->addPlugin($plugin);
115
            }
116
        }
117
118
        return $filesystem;
119
    }
120
}
121