AbstractConfig   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A exists() 0 4 1
A depend() 0 15 3
1
<?php
2
3
namespace ClassConfig;
4
5
use ClassConfig\Exceptions\MissingConfigException;
6
7
/**
8
 * Class Config
9
 * @package ClassConfig
10
 */
11
abstract class AbstractConfig
12
{
13
    /**
14
     * @var object
15
     */
16
    protected $___owner;
17
18
    /**
19
     * @var null|AbstractConfig
20
     */
21
    protected $___parent;
22
23
    /**
24
     * @var null|string
25
     */
26
    protected $___key;
27
28
    /**
29
     * AbstractConfig constructor.
30
     *
31
     * @param object $owner
32
     * @param AbstractConfig|null $parent
33
     * @param string|null $key
34
     */
35
    public function __construct($owner, AbstractConfig $parent = null, string $key = null)
36
    {
37
        $this->___owner = $owner;
38
        $this->___parent = $parent;
39
        $this->___key = $key;
40
    }
41
42
    /**
43
     * @param string $key
44
     * @return bool
45
     */
46
    public function exists(string $key): bool
47
    {
48
        return property_exists($this, '__' . $key . '__');
49
    }
50
51
    /**
52
     * @param string $key
53
     * @return AbstractConfig
54
     * @throws MissingConfigException
55
     */
56
    public function depend(string $key): AbstractConfig
57
    {
58
        if (!isset($this->$key)) {
59
            $trail = [$key];
60
61
            $config = $this;
62
            while ($config->___parent) {
63
                $trail[] = $config->___key;
64
                $config = $config->___parent;
65
            }
66
67
            throw new MissingConfigException(array_reverse($trail));
68
        }
69
        return $this;
70
    }
71
}