ConfigBuilder::addField()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
crap 2.0054
1
<?php
2
3
namespace TreeHouse\Model\Config;
4
5
use TreeHouse\Model\Config\Field\Enum;
6
7
class ConfigBuilder
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $fields = [];
13
14
    /**
15
     * @var array
16
     */
17
    protected $multiValued = [];
18
19
    /**
20
     * @param string $name
21
     * @param string $enumClass
22
     *
23
     * @throws \InvalidArgumentException
24
     */
25 18
    public function addField($name, $enumClass)
26
    {
27 18
        $refl = new \ReflectionClass($enumClass);
28 18
        if (!$refl->isSubclassOf(Enum::class)) {
29
            throw new \InvalidArgumentException(sprintf('%s is not an instance of %s', $enumClass, Enum::class));
30
        }
31
32 18
        $values      = $refl->getMethod('toArray')->invoke(null);
33 18
        $multiValued = $refl->getMethod('isMultiValued')->invoke(null);
34
35 18
        $this->fields[$name]      = array_flip(array_change_key_case($values, CASE_LOWER));
36 18
        $this->multiValued[$name] = $multiValued;
37 18
    }
38
39
    /**
40
     * @return Config
41
     */
42 18
    public function getConfig()
43
    {
44 18
        return new Config($this->fields, $this->multiValued);
45
    }
46
}