Completed
Push — master ( edaba1...b6afd3 )
by Christoffer
01:57
created

ConfigTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setConfig() 0 15 4
A beforeConfig() 0 2 1
A afterConfig() 0 2 1
1
<?php
2
3
namespace Digia\GraphQL\Config;
4
5
trait ConfigTrait
6
{
7
8
    /**
9
     * @var array
10
     */
11
    private $config;
12
13
    /**
14
     * ConfigTrait constructor.
15
     *
16
     * @param array $config
17
     */
18
    public function __construct(array $config = [])
19
    {
20
        $this->beforeConfig();
21
        $this->setConfig($config);
22
        $this->afterConfig();
23
    }
24
25
    /**
26
     * Override this method to perform logic BEFORE configuration is applied.
27
     * This method is useful for setting default values for properties
28
     * that need to use new -keyword.
29
     * If you do, just remember to call the parent implementation.
30
     */
31
    protected function beforeConfig(): void
32
    {
33
    }
34
35
    /**
36
     * Override this method to perform logic AFTER configuration is applied.
37
     * This method is useful for configuring classes after instantiation,
38
     * e.g. adding a query type to a schema or adding fields to object types.
39
     * If you do, just remember to call the parent implementation.
40
     */
41
    protected function afterConfig(): void
42
    {
43
    }
44
45
    /**
46
     * @param array $config
47
     * @return $this
48
     */
49
    protected function setConfig(array $config)
50
    {
51
        foreach ($config as $key => $value) {
52
            $setter = 'set' . ucfirst($key);
53
54
            if (method_exists($this, $setter)) {
55
                $this->$setter($value);
56
            } elseif (property_exists($this, $key)) {
57
                $this->$key = $value;
58
            }
59
        }
60
61
        $this->config = $config;
62
63
        return $this;
64
    }
65
}
66