Completed
Push — master ( 570cf9...050190 )
by Tomáš
01:41
created

EventSourcingConfigurator::configureWithConfig()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 16
cts 16
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
crap 3
1
<?php declare(strict_types = 1);
2
3
namespace TomCizek\SymfonyProoph\EventSourcing;
4
5
use Prooph\EventSourcing\Container\Aggregate\AggregateRepositoryFactory;
6
use Symfony\Component\DependencyInjection\Definition;
7
use Symfony\Component\DependencyInjection\Reference;
8
use TomCizek\SymfonyProoph\Common\DefaultConfigurator;
9
10
class EventSourcingConfigurator extends DefaultConfigurator
11
{
12
	public const KEY = 'event_sourcing';
13
	public const KEY_AGGREGATE_REPOSITORIES = 'aggregate_repository';
14
	public const KEY_REPOSITORY_CLASS = 'repository_class';
15
16 16
	public function buildDefaultConfig(): array
17
	{
18
		return [
19 16
			self::KEY_AGGREGATE_REPOSITORIES => [],
20
		];
21
	}
22
23 16
	public function getConfigKey(): string
24
	{
25 16
		return self::KEY;
26
	}
27
28 14
	public function configureWithConfig(array $config): void
29
	{
30 14
		$repositoriesConfigs = $config[self::KEY_AGGREGATE_REPOSITORIES];
31
32 14
		$interopContainerServiceId = $this->getInteropContainerServiceId();
33
34 14
		foreach ($repositoriesConfigs as $repositoryConfigName => $repositoryConfig) {
35 3
			$repositoryClass = $repositoryConfig[self::KEY_REPOSITORY_CLASS];
36
37 3
			$repositoryDefinition = new Definition($repositoryClass);
38
			$repositoryDefinition
39 3
				->setClass($repositoryClass)
40 3
				->setFactory(AggregateRepositoryFactory::class . '::' . $repositoryConfigName)
41 3
				->addArgument(
42 3
					new Reference($interopContainerServiceId)
43
				);
44
45 3
			$this->containerBuilder->setDefinition('prooph.' . $repositoryConfigName, $repositoryDefinition);
46 3
			$this->containerBuilder->setAlias($repositoryClass, 'prooph.' . $repositoryConfigName);
47
48 3
			$implementations = class_implements($repositoryClass);
49 3
			foreach($implementations as $implementation) {
50 3
				$this->containerBuilder->setAlias($implementation, 'prooph.' . $repositoryConfigName);
51
			}
52
		}
53 14
	}
54
}
55