Passed
Push — main ( bf7881...b4ce3e )
by Sebastian
14:45
created

Util::getEnv()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of SebastianFeldmann\Git.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Runner;
13
14
/**
15
 * Class Util
16
 *
17
 * @package CaptainHook\App\Runner
18
 */
19
final class Util
20
{
21
    /**
22
     * List of valid action types
23
     *
24
     * @var array<bool>
25
     */
26
    private static $validTypes = ['php' => true, 'cli' => true];
27
28
29
    /**
30
     * Check the validity of a exec type
31
     *
32
     * @param  string $type
33
     * @return bool
34
     */
35 1
    public static function isTypeValid(string $type): bool
36
    {
37 1
        return isset(self::$validTypes[$type]);
38
    }
39
40
    /**
41
     * Return action type
42
     *
43
     * @param  string $action
44
     * @return string
45
     */
46 33
    public static function getExecType(string $action): string
47
    {
48 33
        return substr($action, 0, 1) === '\\' ? 'php' : 'cli';
49
    }
50
51
    /**
52
     * Try to read an environment variable
53
     *
54
     * @param  string $name
55
     * @param  string $default
56
     * @return string
57
     */
58 20
    public static function getEnv(string $name, string $default = ''): string
59
    {
60 20
        $var = getenv($name);
61 20
        return $var ?: $_ENV[$name] ?? $_SERVER[$name] ?? $default;
62
    }
63
}
64