Completed
Push — master ( 60fa13...455325 )
by Bas
15:24
created

AbstractAdapterFactory::setCreationOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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