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

AdapterManagerFactory::__invoke()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 21
cts 21
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 14
nop 3
crap 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BsbFlysystem\Service\Factory;
6
7
use BsbFlysystem\Exception\UnexpectedValueException;
8
use BsbFlysystem\Service\AdapterManager;
9
use Interop\Container\ContainerInterface;
10
use Zend\ServiceManager\FactoryInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
use Zend\Stdlib\ArrayUtils;
13
14
class AdapterManagerFactory 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...
15
{
16
    /**
17
     * @var array mapping adapter types to plugin configuration
18
     */
19
    protected $adapterMap = [
20
        'factories' => [
21
            'awss3v3'    => \BsbFlysystem\Adapter\Factory\AwsS3v3AdapterFactory::class,
22
            'azure'      => \BsbFlysystem\Adapter\Factory\AzureAdapterFactory::class,
23
            'dropbox'    => \BsbFlysystem\Adapter\Factory\DropboxAdapterFactory::class,
24
            'ftp'        => \BsbFlysystem\Adapter\Factory\FtpAdapterFactory::class,
25
            'local'      => \BsbFlysystem\Adapter\Factory\LocalAdapterFactory::class,
26
            'rackspace'  => \BsbFlysystem\Adapter\Factory\RackspaceAdapterFactory::class,
27
            'replicate'  => \BsbFlysystem\Adapter\Factory\ReplicateAdapterFactory::class,
28
            'sftp'       => \BsbFlysystem\Adapter\Factory\SftpAdapterFactory::class,
29
            'webdav'     => \BsbFlysystem\Adapter\Factory\WebDAVAdapterFactory::class,
30
            'ziparchive' => \BsbFlysystem\Adapter\Factory\ZipArchiveAdapterFactory::class,
31
            'vfs'        => \BsbFlysystem\Adapter\Factory\VfsAdapterFactory::class,
32
            'null'       => \Zend\ServiceManager\Factory\InvokableFactory::class,
33
        ],
34
        'aliases' => [
35
            'null' => \League\Flysystem\Adapter\NullAdapter::class,
36
        ],
37
    ];
38
39 1
    public function createService(ServiceLocatorInterface $serviceLocator): AdapterManager
40
    {
41 1
        if (method_exists($serviceLocator, 'getServiceLocator')) {
42
            $serviceLocator = $serviceLocator->getServiceLocator();
43
        }
44
45 1
        return $this($serviceLocator, null);
46
    }
47
48 5
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): AdapterManager
49
    {
50 5
        $config        = $container->get('config');
51 5
        $config        = $config['bsb_flysystem'];
52 5
        $serviceConfig = $config['adapter_manager']['config'] ?? [];
53 5
        $adapterMap    = $this->adapterMap;
54
55 5
        if (isset($config['adapter_map'])) {
56 1
            $adapterMap = ArrayUtils::merge($this->adapterMap, $config['adapter_map']);
57
        }
58
59 5
        foreach ($config['adapters'] as $name => $adapterConfig) {
60 4
            if (! isset($adapterConfig['type'])) {
61 1
                throw new UnexpectedValueException(sprintf(
62 1
                    "Missing 'type' key for the adapter '%s' configuration",
63 1
                    $name
64
                ));
65
            }
66
67 3
            $type = strtolower($adapterConfig['type']);
68
69 3
            if (! in_array($type, array_keys($adapterMap['factories']), false)) {
70 1
                throw new UnexpectedValueException(sprintf("Unknown adapter type '%s'", $type));
71
            }
72
73 2
            foreach (array_keys($adapterMap) as $serviceKind) {
74 2
                if (isset($adapterMap[$serviceKind][$type])) {
75 2
                    $serviceConfig[$serviceKind][$name] = $adapterMap[$serviceKind][$type];
76
77 2
                    if (isset($adapterConfig['shared'])) {
78 2
                        $serviceConfig['shared'][$name] = filter_var($adapterConfig['shared'], FILTER_VALIDATE_BOOLEAN);
79
                    }
80
                }
81
            }
82
        }
83
84 3
        return new AdapterManager($container, $serviceConfig);
85
    }
86
}
87