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

AbstractAdapterFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 117
Duplicated Lines 6.84 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 13.95%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 8
loc 117
ccs 6
cts 43
cp 0.1395
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCreationOptions() 0 4 1
A __invoke() 0 14 2
A createService() 8 8 2
A mergeMvcConfig() 0 15 4
B getLazyFactory() 0 33 7
doCreateService() 0 1 ?
validateConfig() 0 1 ?

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\Adapter\Factory;
6
7
use InvalidArgumentException;
8
use League\Flysystem\AdapterInterface;
9
use ProxyManager\Configuration;
10
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
11
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
12
use Psr\Container\ContainerInterface;
13
use Zend\Stdlib\ArrayUtils;
14
15
abstract class AbstractAdapterFactory
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $options;
21
22
    /**
23
     * AbstractAdapterFactory constructor.
24
     *
25
     * @param array $options
26
     */
27 45
    public function __construct(array $options = [])
28
    {
29 45
        $this->setCreationOptions($options);
30 45
    }
31
32
    /**
33
     * Set creation options.
34
     */
35 45
    public function setCreationOptions(array $options)
36
    {
37 45
        $this->options = $options;
38 45
    }
39
40
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): AdapterInterface
41
    {
42
        if (null !== $options) {
43
            $this->setCreationOptions($options);
44
        }
45
46
        $this->mergeMvcConfig($container, $requestedName);
47
48
        $this->validateConfig();
49
50
        $service = $this->doCreateService($container);
51
52
        return $service;
53
    }
54
55 View Code Duplication
    public function createService(ContainerInterface $container): AdapterInterface
56
    {
57
        if (method_exists($container, 'getServiceLocator')) {
58
            $container = $container->getServiceLocator();
59
        }
60
61
        return $this($container, func_get_arg(2));
62
    }
63
64
    /**
65
     * Merges the options given from the ServiceLocator Config object with the create options of the class.
66
     */
67
    protected function mergeMvcConfig(ContainerInterface $container, string $requestedName = null)
68
    {
69
        $config = $container->has('config') ? $container->get('config') : [];
70
71
        if (! isset($config['bsb_flysystem']['adapters'][$requestedName]['options']) ||
72
            ! is_array(($config['bsb_flysystem']['adapters'][$requestedName]['options']))
73
        ) {
74
            return;
75
        }
76
77
        $this->options = ArrayUtils::merge(
78
            $config['bsb_flysystem']['adapters'][$requestedName]['options'],
79
            $this->options
80
        );
81
    }
82
83
    /**
84
     * @throws InvalidArgumentException
85
     */
86
    public function getLazyFactory(ContainerInterface $container): LazyLoadingValueHolderFactory
87
    {
88
        $config = $container->has('config') ? $container->get('config') : [];
89
90
        $config['lazy_services'] = ArrayUtils::merge(
91
            $config['lazy_services'] ?? [],
92
            $config['bsb_flysystem']['adapter_manager']['lazy_services']
93
        );
94
95
        if (! isset($config['lazy_services'])) {
96
            throw new \InvalidArgumentException('Missing "lazy_services" config key');
97
        }
98
99
        $lazyServices = $config['lazy_services'];
100
101
        $factoryConfig = new Configuration();
102
103
        if (isset($lazyServices['proxies_namespace'])) {
104
            $factoryConfig->setProxiesNamespace($lazyServices['proxies_namespace']);
105
        }
106
107
        if (isset($lazyServices['proxies_target_dir'])) {
108
            $factoryConfig->setProxiesTargetDir($lazyServices['proxies_target_dir']);
109
        }
110
111
        if (! isset($lazyServices['write_proxy_files']) || ! $lazyServices['write_proxy_files']) {
112
            $factoryConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
113
        }
114
115
        spl_autoload_register($factoryConfig->getProxyAutoloader());
116
117
        return new LazyLoadingValueHolderFactory($factoryConfig);
118
    }
119
120
    /**
121
     * Create service.
122
     */
123
    abstract protected function doCreateService(ContainerInterface $container): AdapterInterface;
124
125
    /**
126
     * Implement in adapter.
127
     *
128
     * @throw UnexpectedValueException
129
     */
130
    abstract protected function validateConfig();
131
}
132