Environment   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 12
c 1
b 0
f 0
dl 0
loc 43
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 2
A is() 0 3 1
A __construct() 0 3 1
A createWithEnvFile() 0 3 1
A has() 0 3 1
A get() 0 7 2
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