Completed
Push — master ( 60fa13...455325 )
by Bas
15:24
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 90.7%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 8
loc 117
ccs 39
cts 43
cp 0.907
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
C 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 Interop\Container\ContainerInterface;
8
use InvalidArgumentException;
9
use League\Flysystem\AdapterInterface;
10
use ProxyManager\Configuration;
11
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
12
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
13
use Zend\ServiceManager\FactoryInterface;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
use Zend\Stdlib\ArrayUtils;
16
17
abstract class AbstractAdapterFactory 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...
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $options;
23
24
    /**
25
     * AbstractAdapterFactory constructor.
26
     *
27
     * @param array $options
28
     */
29 58
    public function __construct(array $options = [])
30
    {
31 58
        $this->setCreationOptions($options);
32 58
    }
33
34
    /**
35
     * Set creation options.
36
     */
37 58
    public function setCreationOptions(array $options)
38
    {
39 58
        $this->options = $options;
40 58
    }
41
42 10
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): AdapterInterface
43
    {
44 10
        if (null !== $options) {
45
            $this->setCreationOptions($options);
46
        }
47
48 10
        $this->mergeMvcConfig($container, $requestedName);
0 ignored issues
show
Compatibility introduced by
$container of type object<Interop\Container\ContainerInterface> is not a sub-type of object<Zend\ServiceManag...erviceLocatorInterface>. It seems like you assume a child interface of the interface Interop\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
49
50 10
        $this->validateConfig();
51
52 10
        $service = $this->doCreateService($container);
0 ignored issues
show
Compatibility introduced by
$container of type object<Interop\Container\ContainerInterface> is not a sub-type of object<Zend\ServiceManag...erviceLocatorInterface>. It seems like you assume a child interface of the interface Interop\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
53
54 10
        return $service;
55
    }
56
57 2 View Code Duplication
    public function createService(ServiceLocatorInterface $serviceLocator): AdapterInterface
58
    {
59 2
        if (method_exists($serviceLocator, 'getServiceLocator')) {
60 2
            $serviceLocator = $serviceLocator->getServiceLocator();
61
        }
62
63 2
        return $this($serviceLocator, func_get_arg(2));
64
    }
65
66
    /**
67
     * Merges the options given from the ServiceLocator Config object with the create options of the class.
68
     */
69 13
    protected function mergeMvcConfig(ServiceLocatorInterface $serviceLocator, string $requestedName = null)
70
    {
71 13
        $config = $serviceLocator->has('config') ? $serviceLocator->get('config') : [];
72
73 13
        if (! isset($config['bsb_flysystem']['adapters'][$requestedName]['options']) ||
74 13
            ! is_array(($config['bsb_flysystem']['adapters'][$requestedName]['options']))
75
        ) {
76 2
            return;
77
        }
78
79 11
        $this->options = ArrayUtils::merge(
80 11
            $config['bsb_flysystem']['adapters'][$requestedName]['options'],
81 11
            $this->options
82
        );
83 11
    }
84
85
    /**
86
     * @throws InvalidArgumentException
87
     */
88 1
    public function getLazyFactory(ServiceLocatorInterface $serviceLocator): LazyLoadingValueHolderFactory
89
    {
90 1
        $config = $serviceLocator->has('config') ? $serviceLocator->get('config') : [];
91
92 1
        $config['lazy_services'] = ArrayUtils::merge(
93 1
            $config['lazy_services'] ?? [],
94 1
            $config['bsb_flysystem']['adapter_manager']['lazy_services']
95
        );
96
97 1
        if (! isset($config['lazy_services'])) {
98
            throw new \InvalidArgumentException('Missing "lazy_services" config key');
99
        }
100
101 1
        $lazyServices = $config['lazy_services'];
102
103 1
        $factoryConfig = new Configuration();
104
105 1
        if (isset($lazyServices['proxies_namespace'])) {
106
            $factoryConfig->setProxiesNamespace($lazyServices['proxies_namespace']);
107
        }
108
109 1
        if (isset($lazyServices['proxies_target_dir'])) {
110
            $factoryConfig->setProxiesTargetDir($lazyServices['proxies_target_dir']);
111
        }
112
113 1
        if (! isset($lazyServices['write_proxy_files']) || ! $lazyServices['write_proxy_files']) {
114 1
            $factoryConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
115
        }
116
117 1
        spl_autoload_register($factoryConfig->getProxyAutoloader());
118
119 1
        return new LazyLoadingValueHolderFactory($factoryConfig);
120
    }
121
122
    /**
123
     * Create service.
124
     */
125
    abstract protected function doCreateService(ServiceLocatorInterface $serviceLocator): AdapterInterface;
126
127
    /**
128
     * Implement in adapter.
129
     *
130
     * @throw UnexpectedValueException
131
     */
132
    abstract protected function validateConfig();
133
}
134