Passed
Pull Request — master (#149)
by Pierre
03:06
created

Value::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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