Passed
Branch refactor_config_class (788a46)
by Jens
08:38
created

ConfigObject   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 45
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A camelize() 0 10 1
A of() 0 3 1
A fromArray() 0 15 2
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core;
7
8
use Commercetools\Core\Error\InvalidArgumentException;
9
use Commercetools\Core\Error\Message;
10
11
abstract class ConfigObject
12
{
13
    /**
14
     * @param array $configValues
15
     * @return static
16
     */
17
    public static function fromArray(array $configValues)
18
    {
19
        $config = static::of();
20
        array_walk(
21
            $configValues,
22
            function ($value, $key) use ($config) {
23
                $functionName = 'set' . $config->camelize($key);
24
                if (!is_callable([$config, $functionName])) {
25
                    throw new InvalidArgumentException(sprintf(Message::SETTER_NOT_IMPLEMENTED, $key));
26
                }
27
                $config->$functionName($value);
28
            }
29
        );
30
31
        return $config;
32
    }
33
34
    protected function camelize($scored)
35
    {
36
        return lcfirst(
37
            implode(
38
                '',
39
                array_map(
40
                    'ucfirst',
41
                    array_map(
42
                        'strtolower',
43
                        explode('_', $scored)
44
                    )
45
                )
46
            )
47
        );
48
    }
49
50
    /**
51
     * @return static
52
     */
53
    public static function of()
54
    {
55
        return new static();
56
    }
57
}
58