Aggregate::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
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