AppGuesser::determineEnvironment()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
4
namespace Apie\ApplicationInfoPlugin\Guesser;
5
6
use Apie\Core\Apie;
7
8
/**
9
 * Guess the values for ApplicationInfo. It's better to provide them to the plugin though for performance reasons.
10
 */
11
final class AppGuesser
12
{
13
    public static function determineApp(): string
14
    {
15
        $name = 'Apie ';
16
        $version = Apie::VERSION;
17
        $locations = [
18
            __DIR__ . '/../../../../../composer.json',
19
            __DIR__ . '/../../composer.json',
20
        ];
21
        foreach ($locations as $location) {
22
            if (is_readable($location)) {
23
                $result = json_decode(file_get_contents($location), true);
24
                if (is_array($result)) {
25
                    $name = $result['name'] ?? $name;
26
                    $version = $result['version'] ?? $version;
27
                    return $name . ' ' . $version;
28
                }
29
            }
30
        }
31
        return $name . $version;
32
    }
33
34
    public static function determineEnvironment(bool $debug = false): string
35
    {
36
        $envs = ['env', 'environment', 'ENV', 'ENVIRONMENT', 'APP_ENV'];
37
        foreach ($envs as $env) {
38
            if (isset($_ENV[$env])) {
39
                return $_ENV[$env];
40
            }
41
        }
42
        return $debug ? 'dev' : 'prod';
43
    }
44
45
    public static function determineHash(): string
46
    {
47
        $locations = [
48
            __DIR__ . '/../../../../../.git/ORIG_HEAD',
49
            __DIR__ . '/../../.git/ORIG_HEAD',
50
        ];
51
        foreach ($locations as $location) {
52
            if (is_readable($location)) {
53
                $result = trim(file_get_contents($location));
54
                if ($result) {
55
                    return $result;
56
                }
57
            }
58
        }
59
        return '-';
60
    }
61
}
62