Completed
Push — master ( 0339b7...e81256 )
by Bas
7s
created

AbstractAdapterFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 2
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 61
    public function __construct(array $options = [])
29
    {
30 61
        $this->setCreationOptions($options);
31 61
    }
32
33
    /**
34
     * Set creation options
35
     *
36
     * @param  array $options
37
     * @return void
38
     */
39 61
    public function setCreationOptions(array $options)
40
    {
41 61
        $this->options = $options;
42 61
    }
43
44
    /**
45
     * @param ContainerInterface $container
46
     * @param                    $requestedName
47
     * @param array|null         $options
48
     * @return AdapterInterface|VirtualProxyInterface
49
     */
50 11
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
51
    {
52 11
        if (null !== $options) {
53 1
            $this->setCreationOptions($options);
54 1
        }
55
56 11
        $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 11
        $this->validateConfig();
59
60 11
        $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 11
        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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 14
    protected function mergeMvcConfig(ServiceLocatorInterface $serviceLocator, $requestedName)
87
    {
88 14
        $config = $serviceLocator->has('config') ? $serviceLocator->get('config') : [];
89
90 14
        if (!isset($config['bsb_flysystem']['adapters'][$requestedName]['options']) ||
91 12
            !is_array(($config['bsb_flysystem']['adapters'][$requestedName]['options']))
92 14
        ) {
93 2
            return;
94
        }
95
96 12
        $this->options = ArrayUtils::merge(
97 12
            $config['bsb_flysystem']['adapters'][$requestedName]['options'],
98 12
            $this->options
99 12
        );
100 12
    }
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 1
        );
115
116 1
        if (!isset($config['lazy_services'])) {
117
            throw new \InvalidArgumentException('Missing "lazy_services" config key');
118 1
        }
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 1
        }
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