Passed
Push — master ( 97692f...8a9378 )
by Ioannes
01:59
created

EnvHelper::getBxCrontabPeriod()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 3
nc 2
nop 0
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
    const BX_CRONTAB_SINGLE_MODE = false;
14
    const BX_CRONTAB_TIMEOUT = 600;
15
    const BX_CRONTAB_PERIOD = 60;
16
17
    public static function loadEnv() {
18
19
        $envFile = realpath(__DIR__ . '/../../../../.env');
20
        if(!is_file($envFile)) {
21
            $envFile = realpath(__DIR__ . '/../../../../../.env');
22
        }
23
        if(is_file($envFile)) {
24
            try {
25
                $env = new \Symfony\Component\Dotenv\Dotenv();
26
                $env->load($envFile);
27
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
28
29
            }
30
        }
31
    }
32
33
    /**
34
     * @return false|string
35
     */
36
    public static function getDocumentRoot() {
37
38
        $_SERVER['DOCUMENT_ROOT'] = realpath(__DIR__ . '/../../../../');
39
40
        if(isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) {
41
            $_SERVER['DOCUMENT_ROOT'] = $_ENV['APP_DOCUMENT_ROOT'];
42
            return $_SERVER['DOCUMENT_ROOT'];
43
        }
44
45
        $composerFile = realpath(__DIR__ . '/../../../../composer.json');
46
        if(is_file($composerFile)) {
47
            $composerConfig = json_decode(file_get_contents($composerFile), true);
48
            if(isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) {
49
                $_SERVER['DOCUMENT_ROOT'] = $composerConfig['extra']['document-root'];
50
                return $_SERVER['DOCUMENT_ROOT'];
51
            }
52
        }
53
54
        return $_SERVER['DOCUMENT_ROOT'];
55
    }
56
57
    /**
58
     * @param $channel
59
     * @return false|LoggerInterface
60
     */
61
    public static function getLogger($channel) {
62
63
        if(isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) {
64
            $logClass = $_ENV['APP_LOG_CLASS'];
65
            $log = new $logClass($channel);
66
            if($log instanceof LoggerInterface) {
67
                return $log;
68
            }
69
        }
70
71
        return false;
72
    }
73
74
    /**
75
     * @return mixed|string
76
     */
77
    public static function getCrontabFile() {
78
79
        if(isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) {
80
            return rtrim($_ENV['BX_CRONTAB_FOLDER'], "/") . '/bx_crontab.json';
81
        }
82
83
        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

83
        return /** @scrutinizer ignore-type */ self::getDocumentRoot() . self::CRON_TAB_FILE;
Loading history...
84
    }
85
86
    public function getCrontabTimeout() {
87
88
        if(isset($_ENV['BX_CRONTAB_FOLDER']) && is_numeric($_ENV['BX_CRONTAB_FOLDER'])) {
89
            return (int) $_ENV['BX_CRONTAB_FOLDER'];
90
        }
91
92
        return self::BX_CRONTAB_TIMEOUT;
93
    }
94
95
    public function getBxCrontabPeriod() {
96
97
        if(isset($_ENV['BX_CRONTAB_PERIOD']) && is_numeric($_ENV['BX_CRONTAB_PERIOD'])) {
98
            return (int) $_ENV['BX_CRONTAB_PERIOD'];
99
        }
100
101
        return self::BX_CRONTAB_PERIOD;
102
    }
103
104
    public static function getSwitch($name, $state) {
105
106
        if(isset($_ENV[$name])) {
107
108
            $val = strtolower(trim($_ENV[$name]));
109
            if($state == self::SWITCH_STATE_ON) {
110
                if($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') {
111
                    return true;
112
                }
113
            } else if($state == self::SWITCH_STATE_OFF) {
114
                if($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') {
115
                    return true;
116
                }
117
            }
118
        }
119
120
        return false;
121
    }
122
}