UnitOfWorkMapProvisioner   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 23
ccs 0
cts 11
cp 0
rs 10
c 1
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A provision() 0 21 2
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\EventSourcing\EventStore\Storage\StreamStorageInterface;
15
use Daikon\EventSourcing\EventStore\Storage\StreamStorageMap;
16
use Daikon\EventSourcing\EventStore\UnitOfWork;
17
use Daikon\Interop\Assertion;
18
19
final class UnitOfWorkMapProvisioner implements ProvisionerInterface
20
{
21
    public function provision(
22
        Injector $injector,
23
        ConfigProviderInterface $configProvider,
24
        ServiceDefinitionInterface $serviceDefinition
25
    ): void {
26
        $serviceClass = $serviceDefinition->getServiceClass();
27
        $uowConfigs = (array)$configProvider->get('databases.units_of_work', []);
28
        $factory = function (StreamStorageMap $streamStorageMap) use ($uowConfigs, $serviceClass): object {
29
            $unitsOfWork = [];
30
            foreach ($uowConfigs as $uowKey => $uowConfig) {
31
                Assertion::keyNotExists($unitsOfWork, $uowKey, "Unit of work '$uowKey' is already defined.");
32
                /** @var StreamStorageInterface $streamStorage */
33
                $streamStorage = $streamStorageMap->get((string)$uowConfig['stream_store']);
34
                $unitsOfWork[$uowKey] = new UnitOfWork($uowConfig['aggregate_root'], $streamStorage);
35
            }
36
            return new $serviceClass($unitsOfWork);
37
        };
38
39
        $injector
40
            ->share($serviceClass)
41
            ->delegate($serviceClass, $factory);
42
    }
43
}
44