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

AdapterManagerFactory::__invoke()   D

Complexity

Conditions 9
Paths 28

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
ccs 26
cts 26
cp 1
rs 4.909
cc 9
eloc 21
nc 28
nop 3
crap 9
1
<?php
2
3
namespace BsbFlysystem\Service\Factory;
4
5
use BsbFlysystem\Exception\UnexpectedValueException;
6
use BsbFlysystem\Service\AdapterManager;
7
use Interop\Container\ContainerInterface;
8
use Zend\ServiceManager\FactoryInterface;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
use Zend\Stdlib\ArrayUtils;
11
12
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...
13
{
14
    /**
15
     * @var array mapping adapter types to plugin configuration
16
     */
17
    protected $adapterMap = [
18
        'factories' => [
19
            'awss3'      => \BsbFlysystem\Adapter\Factory\AwsS3AdapterFactory::class,
20
            'awss3v3'    => \BsbFlysystem\Adapter\Factory\AwsS3v3AdapterFactory::class,
21
            'azure'      => \BsbFlysystem\Adapter\Factory\AzureAdapterFactory::class,
22
            'copy'       => \BsbFlysystem\Adapter\Factory\CopyAdapterFactory::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
    /**
40
     * Create service
41
     *
42
     * @param ServiceLocatorInterface $serviceLocator
43
     * @return AdapterManager
44
     */
45 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...
46
    {
47
        if (method_exists($serviceLocator, 'getServiceLocator')) {
48
            $serviceLocator = $serviceLocator->getServiceLocator();
49
        }
50
51
        return $this($serviceLocator, null);
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 5
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
58
    {
59 5
        $config        = $container->get('config');
60 5
        $config        = $config['bsb_flysystem'];
61 5
        $serviceConfig = isset($config['adapter_manager']['config']) ? $config['adapter_manager']['config'] : [];
62 5
        $adapterMap    = $this->adapterMap;
63
64 5
        if (isset($config['adapter_map'])) {
65 1
            $adapterMap = ArrayUtils::merge($this->adapterMap, $config['adapter_map']);
66 1
        }
67
68 5
        foreach ($config['adapters'] as $name => $adapterConfig) {
69 4
            if (!isset($adapterConfig['type'])) {
70 1
                throw new UnexpectedValueException(sprintf(
71 1
                    "Missing 'type' key for the adapter '%s' configuration",
72
                    $name
73 1
                ));
74
            }
75
76 3
            $type = strtolower($adapterConfig['type']);
77
78 3
            if (!in_array($type, array_keys($adapterMap['factories']), false)) {
79 1
                throw new UnexpectedValueException(sprintf("Unknown adapter type '%s'", $type));
80
            }
81
82 2
            foreach (array_keys($adapterMap) as $serviceKind) {
83 2
                if (isset($adapterMap[$serviceKind][$type])) {
84 2
                    $serviceConfig[$serviceKind][$name] = $adapterMap[$serviceKind][$type];
85
86 2
                    if (isset($adapterConfig['shared'])) {
87 2
                        $serviceConfig['shared'][$name] = filter_var($adapterConfig['shared'], FILTER_VALIDATE_BOOLEAN);
88 2
                    }
89 2
                }
90 2
            }
91 3
        }
92
93 3
        return new AdapterManager($container, $serviceConfig);
94
    }
95
}
96