Tier   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 1
dl 0
loc 134
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A get() 0 4 1
A __toString() 0 4 1
A getApplicationTiers() 0 4 1
A jsonSerialize() 0 7 1
C validateDefinition() 0 22 7
1
<?php
2
3
namespace Pachico\Tier;
4
5
use Pachico\Tier\Exception;
6
7
/**
8
 * @see https://en.wikipedia.org/wiki/Deployment_environment
9
 */
10
class Tier implements \JsonSerializable
11
{
12
    /**
13
     * Developer's desktop/workstation
14
     */
15
    const LOCAL = 'local';
16
17
    /**
18
     * Development server aka sandbox. This is where unit testing is performed by the developer.
19
     */
20
    const DEVELOPMENT = 'development';
21
22
    /**
23
     * CI build target, or for developer testing of side effects
24
     */
25
    const INTEGRATION = 'integration';
26
27
    /**
28
     * This is the stage where interface testing is performed.
29
     * Quality analysis team make sure that the new code will not have any impact on the existing
30
     * functionality and they test major functionalities of the system once after deploying the
31
     * new code in their respective environment(i.e. QA environment)
32
     */
33
    const TEST = 'test';
34
35
    /**
36
     * Mirror of production environment
37
     */
38
    const STAGING = 'staging';
39
40
    /**
41
     * Serves end-users/clients
42
     */
43
    const PRODUCTION = 'production';
44
45
    /**
46
     * @var array
47
     */
48
    private $allPossibleTiers;
49
50
    /**
51
     * @var string
52
     */
53
    private $tier;
54
55
    /**
56
     * @var array
57
     */
58
    private $applicationTiers;
59
60
    /**
61
     * @param string $tier
62
     * @param array|null $applicationTiers
63
     */
64
    public function __construct($tier, array $applicationTiers = null)
65
    {
66
        $this->allPossibleTiers = [
67
            static::LOCAL,
68
            static::DEVELOPMENT,
69
            static::INTEGRATION,
70
            static::TEST,
71
            static::STAGING,
72
            static::PRODUCTION
73
        ];
74
75
        $this->applicationTiers = empty($applicationTiers) ? $this->allPossibleTiers : array_unique($applicationTiers);
76
        $this->validateDefinition($tier, $this->applicationTiers);
77
        $this->tier = $tier;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function get()
84
    {
85
        return $this->tier;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function __toString()
92
    {
93
        return $this->get();
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function getApplicationTiers()
100
    {
101
        return $this->applicationTiers;
102
    }
103
104
    /**
105
     * @return array
106
     */
107
    public function jsonSerialize()
108
    {
109
        return [
110
            'tier' => $this->tier,
111
            'applicationTiers' => $this->applicationTiers
112
        ];
113
    }
114
115
    /**
116
     * @param string $tier
117
     * @param array $applicationTiers
118
     *
119
     * @throws Exception\InvalidArgumentException
120
     */
121
    private function validateDefinition($tier, array $applicationTiers)
122
    {
123
        if (!is_string($tier) || empty($tier) || !in_array($tier, $this->allPossibleTiers)) {
124
            throw new Exception\InvalidArgumentException(sprintf('Tier name (%s) is not valid', $tier));
125
        }
126
127
        foreach ($applicationTiers as $applicationTier) {
128
            if (!in_array($applicationTier, $this->allPossibleTiers)) {
129
                throw new Exception\InvalidArgumentException(sprintf(
130
                    'At least one available tier (%s) is invalid',
131
                    $applicationTier
132
                ));
133
            }
134
        }
135
136
        if (!in_array($tier, $applicationTiers)) {
137
            throw new Exception\InvalidArgumentException(sprintf(
138
                'Tier (%s) must be one of available environments',
139
                $tier
140
            ));
141
        }
142
    }
143
}
144