Value::supports()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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
use Knp\DictionaryBundle\Dictionary\Simple;
11
use Knp\DictionaryBundle\ValueTransformer;
12
13
final class Value implements Factory
14
{
15
    public function __construct(private readonly ValueTransformer $valueTransformer) {}
16
17
    /**
18
     * {@inheritdoc}
19
     *
20 5
     * @throw InvalidArgumentException Not able to create a dictionary with the given name
21
     */
22 5
    public function create(string $name, array $config): Dictionary
23 5
    {
24
        if (!isset($config['content'])) {
25
            throw new \InvalidArgumentException(\sprintf(
26
                'The key content for dictionary %s must be set.',
27
                $name
28
            ));
29
        }
30 2
31
        $content = $config['content'];
32 2
        $values  = [];
33 1
34 1
        foreach ($content as $value) {
35
            $values[] = $this->valueTransformer->transform($value);
36
        }
37
38
        return new Simple($name, $values);
39 1
    }
40 1
41
    public function supports(array $config): bool
42 1
    {
43 1
        return isset($config['type']) && 'value' === $config['type'];
44
    }
45
}
46