Passed
Push — master ( 3ce074...b5fd53 )
by Peter
02:06
created

EnvReader::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Config;
6
7
use AbterPhp\Framework\Constant\Env;
8
use Opulence\Environments\Environment;
9
10
class EnvReader
11
{
12
    /**
13
     * @return bool
14
     */
15
    public function isStaging(): bool
16
    {
17
        return Environment::getVar(Env::ENV_NAME) === Environment::STAGING;
18
    }
19
20
    /**
21
     * @return bool
22
     */
23
    public function isTesting(): bool
24
    {
25
        return Environment::getVar(Env::ENV_NAME) === Environment::TESTING;
26
    }
27
28
    /**
29
     * @return bool
30
     */
31
    public function isDevelopment(): bool
32
    {
33
        return Environment::getVar(Env::ENV_NAME) === Environment::DEVELOPMENT;
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function isProduction(): bool
40
    {
41
        return Environment::getVar(Env::ENV_NAME) === Environment::PRODUCTION;
42
    }
43
44
    /**
45
     * @param string      $name
46
     * @param string      $expected
47
     * @param string|null $default
48
     *
49
     * @return bool
50
     */
51
    public function is(string $name, string $expected, ?string $default = null): bool
52
    {
53
        return $this->get($name, $default) === $expected;
54
    }
55
56
    /**
57
     * @param string      $name
58
     * @param string|null $default
59
     *
60
     * @return string|null
61
     */
62
    public function get(string $name, ?string $default = null): ?string
63
    {
64
        $value = Environment::getVar($name, $default);
65
        if (null === $value || false === $value) {
66
            return null;
67
        }
68
69
        return (string)$value;
70
    }
71
72
    /**
73
     * @param string      $name
74
     * @param string|null $value
75
     *
76
     * @return $this
77
     * @deprecated Don't use this method without understanding consequences! No removal is planned.
78
     */
79
    public function set(string $name, ?string $value): EnvReader
80
    {
81
        putenv("$name=$value");
82
        $_ENV[$name]    = $value;
83
        $_SERVER[$name] = $value;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param string $name
90
     *
91
     * @return $this
92
     * @deprecated Don't use this method without understanding consequences! No removal is planned.
93
     */
94
    public function clear(string $name): EnvReader
95
    {
96
        putenv("$name");
97
        unset($_ENV[$name]);
98
        unset($_SERVER[$name]);
99
100
        return $this;
101
    }
102
}
103