Passed
Push — master ( 470f50...6685b9 )
by Ioannes
01:53
created

EnvHelper   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 44
eloc 67
c 6
b 1
f 0
dl 0
loc 149
rs 8.8798

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCrontabTimeout() 0 7 3
B checkSleepInterval() 0 20 8
A timeZoneSet() 0 9 3
A loadEnv() 0 11 4
A getCrontabFile() 0 7 3
A getLogger() 0 11 4
A getBxCrontabPeriod() 0 7 3
B getSwitch() 0 17 10
A getDocumentRoot() 0 19 6

How to fix   Complexity   

Complex Class

Complex classes like EnvHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EnvHelper, and based on these observations, apply Extract Interface, too.

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

84
        return /** @scrutinizer ignore-type */ self::getDocumentRoot() . self::CRON_TAB_FILE;
Loading history...
85
    }
86
87
    public static function getCrontabTimeout() {
88
89
        if(isset($_ENV['BX_CRONTAB_FOLDER']) && is_numeric($_ENV['BX_CRONTAB_FOLDER'])) {
90
            return (int) $_ENV['BX_CRONTAB_FOLDER'];
91
        }
92
93
        return self::BX_CRONTAB_TIMEOUT;
94
    }
95
96
    public static function getBxCrontabPeriod() {
97
98
        if(isset($_ENV['BX_CRONTAB_PERIOD']) && is_numeric($_ENV['BX_CRONTAB_PERIOD'])) {
99
            return (int) $_ENV['BX_CRONTAB_PERIOD'];
100
        }
101
102
        return self::BX_CRONTAB_PERIOD;
103
    }
104
105
    public static function getSwitch($name, $state) {
106
107
        if(isset($_ENV[$name])) {
108
109
            $val = strtolower(trim($_ENV[$name]));
110
            if($state == self::SWITCH_STATE_ON) {
111
                if($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') {
112
                    return true;
113
                }
114
            } else if($state == self::SWITCH_STATE_OFF) {
115
                if($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') {
116
                    return true;
117
                }
118
            }
119
        }
120
121
        return false;
122
    }
123
124
    public static function timeZoneSet() {
125
126
        $timeZone = self::BX_CRONTAB_TIMEZONE;
127
128
        if(isset($_ENV['BX_CRONTAB_TIMEZONE']) && $_ENV['BX_CRONTAB_TIMEZONE']) {
129
            $timeZone = trim($_ENV['BX_CRONTAB_TIMEZONE']);
130
        }
131
132
        date_default_timezone_set($timeZone);
133
    }
134
135
    public static function checkSleepInterval() {
136
137
        if(isset($_ENV['BX_CRONTAB_SLEEP_TIME']) && $_ENV['BX_CRONTAB_SLEEP_TIME']) {
138
            $intervals = explode(',', $_ENV['BX_CRONTAB_SLEEP_TIME']);
139
            foreach($intervals as $interval) {
140
                $times = explode('-', $interval);
141
                if(count($times) != 2) {
142
                    continue;
143
                }
144
                $minTime = Time24::validateTimeString($times[0]);
145
                $maxTime = Time24::validateTimeString($times[1]);
146
                if($minTime && $maxTime) {
147
                    if(Time24::inInterval($minTime, $maxTime)) {
148
                        return $interval;
149
                    }
150
                }
151
            }
152
        }
153
154
        return false;
155
    }
156
}