Environment::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPEnv;
6
7
use RemotelyLiving\PHPEnv\Marshallers;
8
9
final class Environment implements Interfaces\Environment
10
{
11
    private EnvironmentType $environmentType;
12
13
    private function __construct(EnvironmentType $environmentType)
14
    {
15
        $this->environmentType = $environmentType;
16
    }
17
18
    public static function create(EnvironmentType $environmentType, Interfaces\Marshaller $marshaller = null): self
19
    {
20
        if ($marshaller) {
21
            $marshaller->marshallIntoEnvironment();
22
        }
23
24
        return new self($environmentType);
25
    }
26
27
    public static function createWithEnvFile(EnvironmentType $environmentType, string $fullPath): self
28
    {
29
        return self::create($environmentType, Marshallers\EnvFile::createFromPath($fullPath));
30
    }
31
32
    public function is(EnvironmentType $environment): bool
33
    {
34
        return $this->environmentType->equals($environment);
35
    }
36
37
    public function has(string $key): bool
38
    {
39
        return \getenv($key) !== false;
40
    }
41
42
    /**
43
     * @psalm-suppress PossiblyFalseArgument
44
     */
45
    public function get(string $key): Interfaces\Caster
46
    {
47
        if ($this->has($key)) {
48
            return StringCaster::cast(\getenv($key));
49
        }
50
51
        throw Exceptions\RuntimeError::keyNotFound($key);
52
    }
53
}
54