Passed
Push — master ( 3770a6...fe55a7 )
by Ioannes
07:32
created

EnvHelper::getBinPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
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
    const BIN_FILE = '/vendor/bin/bxconsole';
10
    const BIN_LOG_PATH = '/home/bitrix/log/';
11
12
    const SWITCH_STATE_ON = 'on';
13
    const SWITCH_STATE_OFF = 'off';
14
15
    const BX_CRONTAB_SINGLE_MODE = false;
16
    const BX_CRONTAB_TIMEOUT = 600;
17
    const BX_CRONTAB_PERIOD = 60;
18
    const BX_CRONTAB_TIMEZONE = 'Europe/Moscow';
19
20
    public static function loadEnv() {
21
22
        $envFile = realpath(__DIR__ . '/../../../../.env');
23
        if(!is_file($envFile)) {
24
            $envFile = realpath(__DIR__ . '/../../../../../.env');
25
        }
26
        if(is_file($envFile)) {
27
            try {
28
                $env = new \Symfony\Component\Dotenv\Dotenv();
29
                $env->load($envFile);
30
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
31
32
            }
33
        }
34
    }
35
36
    public static function getBinPath() {
37
38
        return pathinfo($_ENV['APP_COMPOSER'], PATHINFO_DIRNAME) . self::BIN_FILE;
0 ignored issues
show
Bug introduced by
Are you sure pathinfo($_ENV['APP_COMP...nsole\PATHINFO_DIRNAME) of type array|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

38
        return /** @scrutinizer ignore-type */ pathinfo($_ENV['APP_COMPOSER'], PATHINFO_DIRNAME) . self::BIN_FILE;
Loading history...
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public static function getDocumentRoot() {
45
46
        if(isset($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['DOCUMENT_ROOT'])) {
47
            return $_SERVER['DOCUMENT_ROOT'];
48
        }
49
50
        if(isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) {
51
            $_SERVER['DOCUMENT_ROOT'] = $_ENV['APP_DOCUMENT_ROOT'];
52
            return $_SERVER['DOCUMENT_ROOT'];
53
        }
54
55
        $composerFile = realpath(__DIR__ . '/../../../../composer.json');
56
        if(is_file($composerFile)) {
57
            $composerConfig = json_decode(file_get_contents($composerFile), true);
58
            if(isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) {
59
                $_SERVER['DOCUMENT_ROOT'] = $composerConfig['extra']['document-root'];
60
                return $_SERVER['DOCUMENT_ROOT'];
61
            }
62
        }
63
64
        $_SERVER['DOCUMENT_ROOT'] = realpath(__DIR__ . '/../../../../');
65
66
        return (string) $_SERVER['DOCUMENT_ROOT'];
67
    }
68
69
    /**
70
     * @param $channel
71
     * @return false|LoggerInterface
72
     */
73
    public static function getLogger($channel) {
74
75
        if(isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) {
76
            $logClass = $_ENV['APP_LOG_CLASS'];
77
            $log = new $logClass($channel);
78
            if($log instanceof LoggerInterface) {
79
                return $log;
80
            }
81
        }
82
83
        return false;
84
    }
85
86
    /**
87
     * @return mixed|string
88
     */
89
    public static function getCrontabFile() {
90
91
        if(isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) {
92
            return rtrim($_ENV['BX_CRONTAB_FOLDER'], "/") . '/bx_crontab.json';
93
        }
94
95
        return self::getDocumentRoot() . self::CRON_TAB_FILE;
96
    }
97
98
    public static function getCrontabTimeout() {
99
100
        if(isset($_ENV['BX_CRONTAB_TIMEOUT']) && is_numeric($_ENV['BX_CRONTAB_TIMEOUT'])) {
101
            return (int) $_ENV['BX_CRONTAB_TIMEOUT'];
102
        }
103
104
        return self::BX_CRONTAB_TIMEOUT;
105
    }
106
107
    public static function getBxCrontabPeriod() {
108
109
        if(isset($_ENV['BX_CRONTAB_PERIOD']) && is_numeric($_ENV['BX_CRONTAB_PERIOD'])) {
110
            return (int) $_ENV['BX_CRONTAB_PERIOD'];
111
        }
112
113
        return self::BX_CRONTAB_PERIOD;
114
    }
115
116
    public static function getSwitch($name, $state) {
117
118
        if(isset($_ENV[$name])) {
119
120
            $val = strtolower(trim($_ENV[$name]));
121
            if($state == self::SWITCH_STATE_ON) {
122
                if($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') {
123
                    return true;
124
                }
125
            } else if($state == self::SWITCH_STATE_OFF) {
126
                if($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') {
127
                    return true;
128
                }
129
            }
130
        }
131
132
        return false;
133
    }
134
135
    public static function timeZoneSet() {
136
137
        $timeZone = self::BX_CRONTAB_TIMEZONE;
138
139
        if(isset($_ENV['BX_CRONTAB_TIMEZONE']) && $_ENV['BX_CRONTAB_TIMEZONE']) {
140
            $timeZone = trim($_ENV['BX_CRONTAB_TIMEZONE']);
141
        }
142
143
        date_default_timezone_set($timeZone);
144
    }
145
146
    public static function checkSleepInterval() {
147
148
        if(isset($_ENV['BX_CRONTAB_SLEEP_TIME']) && $_ENV['BX_CRONTAB_SLEEP_TIME']) {
149
            $intervals = explode(',', $_ENV['BX_CRONTAB_SLEEP_TIME']);
150
            foreach($intervals as $interval) {
151
                $times = explode('-', $interval);
152
                if(count($times) != 2) {
153
                    continue;
154
                }
155
                $minTime = Time24::validateTimeString($times[0]);
156
                $maxTime = Time24::validateTimeString($times[1]);
157
                if($minTime && $maxTime) {
158
                    if(Time24::inInterval($minTime, $maxTime)) {
159
                        return $interval;
160
                    }
161
                }
162
            }
163
        }
164
165
        return false;
166
    }
167
}