Completed
Push — master ( 60fa13...455325 )
by Bas
15:24
created

FilesystemFactory   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 91
Duplicated Lines 10.99 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 93.02%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 7
dl 10
loc 91
ccs 40
cts 43
cp 0.9302
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 58 16

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
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 1
                $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