StreamStorageMapProvisioner::provision()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 1
nop 3
dl 0
loc 28
ccs 0
cts 10
cp 0
crap 6
rs 9.6333
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Service\Provisioner;
10
11
use Auryn\Injector;
12
use Daikon\Boot\Service\ServiceDefinitionInterface;
13
use Daikon\Config\ConfigProviderInterface;
14
use Daikon\Dbal\Storage\StorageAdapterMap;
15
use Daikon\Interop\Assertion;
16
17
final class StreamStorageMapProvisioner implements ProvisionerInterface
18
{
19
    public function provision(
20
        Injector $injector,
21
        ConfigProviderInterface $configProvider,
22
        ServiceDefinitionInterface $serviceDefinition
23
    ): void {
24
        $serviceClass = $serviceDefinition->getServiceClass();
25
        $adapterConfigs = (array)$configProvider->get('databases.stream_stores', []);
26
        $factory = function (
27
            StorageAdapterMap $storageAdapterMap
28
        ) use (
29
            $injector,
30
            $serviceClass,
31
            $adapterConfigs
32
        ): object {
33
            $adapters = [];
34
            foreach ($adapterConfigs as $adapterKey => $adapterConfigs) {
35
                Assertion::keyNotExists($adapters, $adapterKey, "Stream adapter '$adapterKey' is already defined.");
36
                $adapters[$adapterKey] = $injector->make(
37
                    $adapterConfigs['class'],
38
                    [':storageAdapter' => $storageAdapterMap->get($adapterConfigs['storage_adapter'])]
39
                );
40
            }
41
            return new $serviceClass($adapters);
42
        };
43
44
        $injector
45
            ->share($serviceClass)
46
            ->delegate($serviceClass, $factory);
47
    }
48
}
49