Total Complexity | 44 |
Total Lines | 149 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 0 |
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 |
||
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) { |
||
|
|||
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; |
||
85 | } |
||
86 | |||
87 | public static function getCrontabTimeout() { |
||
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() { |
||
155 | } |
||
156 | } |