Completed
Pull Request — master (#42)
by Florent
12:58
created

AbstractAdapterFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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