Completed
Push — master ( a0e8ce...60fa13 )
by Bas
12s
created

AbstractAdapterFactory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 5.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 83.72%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 8
loc 141
ccs 36
cts 43
cp 0.8372
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 mergeMvcConfig() 0 15 4
C getLazyFactory() 0 33 8
doCreateService() 0 1 ?
validateConfig() 0 1 ?
A __invoke() 0 14 2
A createService() 8 8 2

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