AdapterManagerFactory::createService()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * BsbFlystem
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @see       https://bushbaby.nl/
10
 *
11
 * @copyright Copyright (c) 2014-2021 bushbaby multimedia. (https://bushbaby.nl)
12
 * @author    Bas Kamer <[email protected]>
13
 * @license   MIT
14
 *
15
 * @package   bushbaby/flysystem
16
 */
17
18
declare(strict_types=1);
19
20
namespace BsbFlysystem\Service\Factory;
21
22
use BsbFlysystem\Adapter\Factory\AwsS3v3AdapterFactory;
23
use BsbFlysystem\Adapter\Factory\AzureAdapterFactory;
24
use BsbFlysystem\Adapter\Factory\DropboxAdapterFactory;
25
use BsbFlysystem\Adapter\Factory\FtpAdapterFactory;
26
use BsbFlysystem\Adapter\Factory\FtpdAdapterFactory;
27
use BsbFlysystem\Adapter\Factory\GoogleCloudDriveAdapterFactory;
28
use BsbFlysystem\Adapter\Factory\LocalAdapterFactory;
29
use BsbFlysystem\Adapter\Factory\RackspaceAdapterFactory;
30
use BsbFlysystem\Adapter\Factory\ReplicateAdapterFactory;
31
use BsbFlysystem\Adapter\Factory\SftpAdapterFactory;
32
use BsbFlysystem\Adapter\Factory\VfsAdapterFactory;
33
use BsbFlysystem\Adapter\Factory\WebDAVAdapterFactory;
34
use BsbFlysystem\Adapter\Factory\ZipArchiveAdapterFactory;
35
use BsbFlysystem\Exception\UnexpectedValueException;
36
use BsbFlysystem\Service\AdapterManager;
37
use Laminas\ServiceManager\Factory\InvokableFactory;
38
use Laminas\Stdlib\ArrayUtils;
39
use League\Flysystem\Adapter\NullAdapter;
40
use Psr\Container\ContainerInterface;
41
42
class AdapterManagerFactory
43
{
44
    /**
45
     * @var array mapping adapter types to plugin configuration
46
     */
47
    protected $adapterMap = [
48 5
        'factories' => [
49
            'awss3v3' => AwsS3v3AdapterFactory::class,
50 5
            'azure' => AzureAdapterFactory::class,
51 5
            'dropbox' => DropboxAdapterFactory::class,
52 5
            'ftp' => FtpAdapterFactory::class,
53 5
            'ftpd' => FtpdAdapterFactory::class,
54
            'googlecloudstorage' => GoogleCloudDriveAdapterFactory::class,
55 5
            'local' => LocalAdapterFactory::class,
56 1
            'rackspace' => RackspaceAdapterFactory::class,
57
            'replicate' => ReplicateAdapterFactory::class,
58
            'sftp' => SftpAdapterFactory::class,
59 5
            'webdav' => WebDAVAdapterFactory::class,
60 4
            'ziparchive' => ZipArchiveAdapterFactory::class,
61 1
            'vfs' => VfsAdapterFactory::class,
62 1
            'null' => InvokableFactory::class,
63 1
        ],
64
        'aliases' => [
65
            'null' => NullAdapter::class,
66
        ],
67 3
    ];
68
69 3
    public function createService(ContainerInterface $container): AdapterManager
70 1
    {
71
        if (\method_exists($container, 'getServiceLocator')) {
72
            $container = $container->getServiceLocator();
73 2
        }
74 2
75 2
        return $this($container, null);
76
    }
77 2
78 2
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): AdapterManager
79
    {
80
        $config = $container->get('config');
81
        $config = $config['bsb_flysystem'];
82
        $serviceConfig = $config['adapter_manager']['config'] ?? [];
83
        $adapterMap = $this->adapterMap;
84 3
85
        if (isset($config['adapter_map'])) {
86
            $adapterMap = ArrayUtils::merge($this->adapterMap, $config['adapter_map']);
87
        }
88
89
        foreach ($config['adapters'] as $name => $adapterConfig) {
90
            if (! isset($adapterConfig['type'])) {
91
                throw new UnexpectedValueException(\sprintf("Missing 'type' key for the adapter '%s' configuration", $name));
92
            }
93
94
            $type = \strtolower($adapterConfig['type']);
95
96
            if (! \in_array($type, \array_keys($adapterMap['factories']), false)) {
97
                throw new UnexpectedValueException(\sprintf("Unknown adapter type '%s'", $type));
98
            }
99
100
            foreach (\array_keys($adapterMap) as $serviceKind) {
101
                if (isset($adapterMap[$serviceKind][$type])) {
102
                    $serviceConfig[$serviceKind][$name] = $adapterMap[$serviceKind][$type];
103
104
                    if (isset($adapterConfig['shared'])) {
105
                        $serviceConfig['shared'][$name] = \filter_var($adapterConfig['shared'], FILTER_VALIDATE_BOOLEAN);
106
                    }
107
                }
108
            }
109
        }
110
111
        return new AdapterManager($container, $serviceConfig);
112
    }
113
}
114