Aggregate   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 11 3
A supports() 0 9 3
A addFactory() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Dictionary\Factory;
6
7
use InvalidArgumentException;
8
use Knp\DictionaryBundle\Dictionary;
9
use Knp\DictionaryBundle\Dictionary\Factory;
10
11
final class Aggregate implements Factory
12
{
13
    /**
14
     * @var array<Factory>
15
     */
16
    private array $factories = [];
17
18 3
    public function addFactory(Factory $factory): void
19
    {
20 3
        $this->factories[] = $factory;
21 3
    }
22
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * @throw InvalidArgumentException Not able to create a dictionary with the given name
27
     */
28 2
    public function create(string $name, array $config): Dictionary
29
    {
30 2
        foreach ($this->factories as $factory) {
31 2
            if ($factory->supports($config)) {
32 1
                return $factory->create($name, $config);
33
            }
34
        }
35
36 1
        throw new \InvalidArgumentException(\sprintf(
37 1
            'The dictionary with named "%s" cannot be created.',
38
            $name
39
        ));
40
    }
41
42 1
    public function supports(array $config): bool
43
    {
44 1
        foreach ($this->factories as $factory) {
45 1
            if ($factory->supports($config)) {
46 1
                return true;
47
            }
48
        }
49
50 1
        return false;
51
    }
52
}
53