ConfigurationTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 73
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 8 2
A mapConfiguration() 0 9 1
A parseConfiguration() 0 8 1
A setConfig() 0 12 3
1
<?php
2
3
namespace Stevenmaguire\Yelp\Tool;
4
5
trait ConfigurationTrait
6
{
7
    /**
8
     * Retrives the value of a given property from the client.
9
     *
10
     * @param  string  $property
11
     *
12
     * @return mixed|null
13
     */
14 4
    public function __get($property)
15
    {
16 4
        if (property_exists($this, $property)) {
17 4
            return $this->$property;
18
        }
19
20 4
        return null;
21
    }
22
23
    /**
24
     * Maps legacy configuration keys to updated keys.
25
     *
26
     * @param  array   $configuration
27
     *
28
     * @return array
29
     */
30
    protected function mapConfiguration(array $configuration)
31
    {
32 42
        array_walk($configuration, function ($value, $key) use (&$configuration) {
33 42
            $newKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
34 42
            $configuration[$newKey] = $value;
35 42
        });
36
37 42
        return $configuration;
38
    }
39
40
    /**
41
     * Parse configuration using defaults
42
     *
43
     * @param  array $configuration
44
     * @param  array $defaults
45
     *
46
     * @return mixed
47
     */
48 42
    protected function parseConfiguration($configuration = [], $defaults = [])
49
    {
50 42
        $configuration = array_merge($defaults, $this->mapConfiguration($configuration));
51
52 42
        array_walk($configuration, [$this, 'setConfig']);
53
54 42
        return $this;
55
    }
56
57
    /**
58
     * Attempts to set a given value.
59
     *
60
     * @param mixed   $value
61
     * @param string  $key
62
     *
63
     * @return mixed
64
     */
65 42
    protected function setConfig($value, $key)
66
    {
67 42
        $setter = 'set' . ucfirst($key);
68
69 42
        if (method_exists($this, $setter)) {
70 2
            $this->$setter($value);
71 42
        } elseif (property_exists($this, $key)) {
72 42
            $this->$key = $value;
73 21
        }
74
75 42
        return $this;
76
    }
77
}
78