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 ( 40c845...e97398 )
by Benjamin
02:50
created

EnvVarsSetter::populate()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 8.2327

Importance

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