AbstractAdapterFactory   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 6.96 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

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