Passed
Push — master ( 729b2b...97692f )
by Ioannes
01:50
created

EnvHelper::getSwitch()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
rs 7.6666
cc 10
nc 6
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace App\BxConsole;
3
4
use Psr\Log\LoggerInterface;
5
6
class EnvHelper {
7
8
    const CRON_TAB_FILE = '/bitrix/tmp/bx_crontab.json';
9
10
    const SWITCH_STATE_ON = 'on';
11
    const SWITCH_STATE_OFF = 'off';
12
13
    public static function loadEnv() {
14
15
        $envFile = realpath(__DIR__ . '/../../../../.env');
16
        if(!is_file($envFile)) {
17
            $envFile = realpath(__DIR__ . '/../../../../../.env');
18
        }
19
        if(is_file($envFile)) {
20
            try {
21
                $env = new \Symfony\Component\Dotenv\Dotenv();
22
                $env->load($envFile);
23
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
24
25
            }
26
        }
27
    }
28
29
    /**
30
     * @return false|string
31
     */
32
    public static function getDocumentRoot() {
33
34
        $_SERVER['DOCUMENT_ROOT'] = realpath(__DIR__ . '/../../../../');
35
36
        if(isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) {
37
            $_SERVER['DOCUMENT_ROOT'] = $_ENV['APP_DOCUMENT_ROOT'];
38
            return $_SERVER['DOCUMENT_ROOT'];
39
        }
40
41
        $composerFile = realpath(__DIR__ . '/../../../../composer.json');
42
        if(is_file($composerFile)) {
43
            $composerConfig = json_decode(file_get_contents($composerFile), true);
44
            if(isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) {
45
                $_SERVER['DOCUMENT_ROOT'] = $composerConfig['extra']['document-root'];
46
                return $_SERVER['DOCUMENT_ROOT'];
47
            }
48
        }
49
50
        return $_SERVER['DOCUMENT_ROOT'];
51
    }
52
53
    /**
54
     * @param $channel
55
     * @return false|LoggerInterface
56
     */
57
    public static function getLogger($channel) {
58
59
        if(isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) {
60
            $logClass = $_ENV['APP_LOG_CLASS'];
61
            $log = new $logClass($channel);
62
            if($log instanceof LoggerInterface) {
63
                return $log;
64
            }
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * @return mixed|string
72
     */
73
    public static function getCrontabFile() {
74
75
        if(isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) {
76
            return rtrim($_ENV['BX_CRONTAB_FOLDER'], "/") . '/bx_crontab.json';
77
        }
78
79
        return self::getDocumentRoot() . self::CRON_TAB_FILE;
0 ignored issues
show
Bug introduced by
Are you sure self::getDocumentRoot() of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        return /** @scrutinizer ignore-type */ self::getDocumentRoot() . self::CRON_TAB_FILE;
Loading history...
80
    }
81
82
    public static function getSwitch($name, $state) {
83
84
        if(isset($_ENV[$name])) {
85
86
            $val = strtolower(trim($_ENV[$name]));
87
            if($state == self::SWITCH_STATE_ON) {
88
                if($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') {
89
                    return true;
90
                }
91
            } else if($state == self::SWITCH_STATE_OFF) {
92
                if($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') {
93
                    return true;
94
                }
95
            }
96
        }
97
98
        return false;
99
    }
100
}