Context::isTest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\Aent\Context;
4
5
use TheAentMachine\Aent\Payload\JsonPayloadInterface;
6
use TheAentMachine\Aenthill\Aenthill;
7
8
class Context implements JsonPayloadInterface, ContextInterface
9
{
10
    public const DEV = 'development';
11
    public const TEST = 'test';
12
    public const PROD = 'production';
13
14
    /** @var string */
15
    protected $environmentType;
16
17
    /** @var string */
18
    private $environmentName;
19
20
    /**
21
     * Environment constructor.
22
     * @param string $environmentType
23
     * @param string $environmentName
24
     */
25
    public function __construct(string $environmentType, string $environmentName)
26
    {
27
        $this->environmentType = $environmentType;
28
        $this->environmentName = $environmentName;
29
    }
30
31
    /**
32
     * @return string[]
33
     */
34
    public static function getEnvironmentTypeList(): array
35
    {
36
        return [
37
            self::DEV,
38
            self::TEST,
39
            self::PROD,
40
        ];
41
    }
42
43
    /**
44
     * @return array<string,string>
45
     */
46
    public function toArray(): array
47
    {
48
        return [
49
            'ENVIRONMENT_TYPE' => $this->environmentType,
50
            'ENVIRONMENT_NAME' => $this->environmentName,
51
        ];
52
    }
53
54
    /**
55
     * @param array<string,string> $assoc
56
     * @return mixed
57
     */
58
    public static function fromArray(array $assoc)
59
    {
60
        $environmentType = $assoc['ENVIRONMENT_TYPE'];
61
        $environmentName = $assoc['ENVIRONMENT_NAME'];
62
        return new self($environmentType, $environmentName);
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    public function toMetadata(): void
69
    {
70
        Aenthill::update($this->toArray());
71
    }
72
73
    /**
74
     * @return mixed
75
     */
76
    public static function fromMetadata()
77
    {
78
        $environmentType = Aenthill::metadata('ENVIRONMENT_TYPE');
79
        $environmentName = Aenthill::metadata('ENVIRONMENT_NAME');
80
        return new self($environmentType, $environmentName);
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    public function isDevelopment(): bool
87
    {
88
        return $this->environmentType === self::DEV;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function isTest(): bool
95
    {
96
        return $this->environmentType === self::TEST;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    public function isProduction(): bool
103
    {
104
        return $this->environmentType === self::PROD;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getEnvironmentType(): string
111
    {
112
        return $this->environmentType;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getEnvironmentName(): string
119
    {
120
        return $this->environmentName;
121
    }
122
}
123