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

AdapterManagerFactory::__invoke()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

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