GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 0dd51f...bd31e6 )
by Benjamin
02:53
created

EnvVarsSetter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lib\Env;
6
7
use Lib\Env\Exception\EnvException;
8
use Lib\Env\Exception\PathException;
9
use Lib\Env\Parser\EnvParserInterface;
10
11
final class EnvVarsSetter
12
{
13
    private const DIST_EXT = '.dist';
14
15
    private const ENV_DEV = 'dev';
16
    private const ENV_TEST = 'test';
17
    private const ENV_PROD = 'prod';
18
19
    /** @var array */
20
    private $envVars;
21
22
    /** @var EnvParserInterface */
23
    private $parser;
24
25 2
    public function __construct(EnvParserInterface $parser)
26
    {
27 2
        $this->parser = $parser;
28 2
    }
29
30
    /**
31
     * @param  string       $path
32
     * @param  string       $envVarName
33
     * @param  string       $defaultEnv
34
     * @param  array        $testEnvs
35
     * @throws EnvException
36
     */
37 2
    public function loadEnv(string $path, string $envVarName = 'APP_ENV', string $defaultEnv = self::ENV_DEV, $testEnvs = [self::ENV_TEST]): void
38
    {
39 2
        $file = '';
40 2
        if (null === $env = $_SERVER[$envVarName] ?? $_ENV[$envVarName] ?? null) {
41 2
            $this->envVars[$envVarName] = $env = $defaultEnv;
42
        }
43 2
        if (file_exists($path) && !file_exists($file = $path.self::DIST_EXT)) {
44
            $this->doLoad($path);
45 2
        } elseif (file_exists($file)) {
46
            $this->doLoad($file);
47
        }
48
49 2
        if (!\in_array($env, $testEnvs, true) && file_exists($file = "$path.local")) {
50
            $this->doLoad($file);
51
        }
52 2
        foreach (["$path.$env", "$path.$env.local"] as $file) {
53 2
            if (file_exists($file)) {
54 2
                $this->doLoad($file);
55
            }
56
        }
57 2
    }
58
59
    /**
60
     * @param  string       $path
61
     * @throws EnvException
62
     */
63 1
    public function doLoad(string $path)
64
    {
65 1
        if (!is_readable($path) || is_dir($path)) {
66
            throw new PathException();
67
        }
68
        try {
69 1
            $this->populate($path);
70
        } catch (EnvException $e) {
71
            throw $e;
72
        }
73 1
    }
74
75
    /**
76
     * @param  string       $path
77
     * @param  bool         $override
78
     * @throws EnvException
79
     */
80 1
    private function populate(string $path, bool $override = false): void
81
    {
82
        try {
83 1
            $vars = $this->parser->parse(file_get_contents($path));
84
        } catch (EnvException $e) {
85
            throw $e;
86
        }
87 1
        foreach ($vars as $varName => $value) {
88 1
            $httpVar = 0 !== mb_strpos($varName, 'HTTP_');
89
90 1
            if (!$override && (isset($_ENV[$varName]) || ($httpVar && isset($_SERVER[$varName])))) {
91
                continue;
92
            }
93
94 1
            putenv("$varName=$value");
95 1
            $this->envVars[$varName] = $_ENV[$varName] = $value;
96 1
            if ($httpVar) {
97 1
                $_SERVER[$varName] = $value;
98
            }
99
        }
100 1
    }
101
}
102