ConfigurationContainer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 9
c 6
b 3
f 0
lcom 2
cbo 1
dl 0
loc 96
ccs 22
cts 22
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A getContainerType() 0 4 1
A getValueOrDefault() 0 8 2
A isValid() 0 10 2
checkValidity() 0 1 ?
A getValueOrFail() 0 8 2
1
<?php
2
3
namespace Onigoetz\Deployer\Configuration\Containers;
4
5
use Onigoetz\Deployer\Configuration\ConfigurationManager;
6
7
abstract class ConfigurationContainer
8
{
9
    /**
10
     * @var string The name of the element
11
     */
12
    protected $name;
13
14
    /**
15
     * @var array The data for this container
16
     */
17
    protected $data;
18
19
    /**
20
     * @var \Onigoetz\Deployer\Configuration\ConfigurationManager
21
     */
22
    protected $manager;
23
24 237
    public function __construct($name, array $data, ConfigurationManager $manager)
25
    {
26 237
        $this->name = $name;
27 237
        $this->data = $data;
28 237
        $this->manager = $manager;
29 237
    }
30
31 69
    public function getName()
32
    {
33 69
        return $this->name;
34
    }
35
36
    /**
37
     * Get the configuration manager's key
38
     *
39
     * @return string
40
     */
41 51
    public function getContainerType()
42
    {
43 51
        return strtolower(implode('', array_slice(explode('\\', get_class($this)), -1)));
44
    }
45
46
    /**
47
     * Get the value or throw an exception
48
     *
49
     * @param string $key
50
     * @param $errorMessage
51
     * @throws \LogicException
52
     * @return mixed
53
     */
54 48
    protected function getValueOrFail($key, $errorMessage)
55
    {
56 48
        if (array_key_exists($key, $this->data)) {
57 39
            return $this->data[$key];
58
        }
59
60 15
        throw new \LogicException($errorMessage);
61
    }
62
63
    /**
64
     * Get the value or return the default
65
     *
66
     * @param string $key
67
     * @param $default
68
     * @return mixed
69
     */
70 18
    protected function getValueOrDefault($key, $default)
71
    {
72 18
        if (array_key_exists($key, $this->data)) {
73 12
            return $this->data[$key];
74
        }
75
76 6
        return $default;
77
    }
78
79
    /**
80
     * Tests validity
81
     *
82
     * @return bool
83
     */
84 57
    public function isValid()
85
    {
86
        try {
87 57
            return $this->checkValidity();
88 30
        } catch (\LogicException $e) {
89 30
            $this->manager->log($e->getMessage());
90
91 30
            return false;
92
        }
93
    }
94
95
    /**
96
     * Internal validity check
97
     *
98
     * @throws \LogicException
99
     * @return bool
100
     */
101
    abstract public function checkValidity();
102
}
103