Passed
Pull Request — master (#24)
by Damian
01:51
created

RequiresConfig::getEnv()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Firesphere\GraphQLJWT\Helpers;
4
5
use LogicException;
6
use SilverStripe\Core\Environment;
7
8
trait RequiresConfig
9
{
10
    /**
11
     * Get an environment value. If $default is not set and the environment isn't set either this will error.
12
     *
13
     * @param string $key
14
     * @param mixed $default
15
     * @throws LogicException Error if environment variable is required, but not configured
16
     * @return mixed
17
     */
18
    public static function getEnv(string $key, $default = null)
19
    {
20
        $value = Environment::getEnv($key);
21
        if ($value) {
22
            return $value;
23
        }
24
        if (func_num_args() === 1) {
25
            throw new LogicException("Required environment variable {$key} not set");
26
        }
27
        return $default;
28
    }
29
}
30