| Total Complexity | 46 |
| Total Lines | 153 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| 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() { |
||
| 29 | |||
| 30 | } |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | public static function getDocumentRoot() { |
||
| 38 | |||
| 39 | if(isset($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['DOCUMENT_ROOT'])) { |
||
| 40 | return $_SERVER['DOCUMENT_ROOT']; |
||
| 41 | } |
||
| 42 | |||
| 43 | $_SERVER['DOCUMENT_ROOT'] = realpath(__DIR__ . '/../../../../'); |
||
| 44 | |||
| 45 | if(isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) { |
||
| 46 | $_SERVER['DOCUMENT_ROOT'] = $_ENV['APP_DOCUMENT_ROOT']; |
||
| 47 | return $_SERVER['DOCUMENT_ROOT']; |
||
| 48 | } |
||
| 49 | |||
| 50 | $composerFile = realpath(__DIR__ . '/../../../../composer.json'); |
||
| 51 | if(is_file($composerFile)) { |
||
| 52 | $composerConfig = json_decode(file_get_contents($composerFile), true); |
||
| 53 | if(isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) { |
||
| 54 | $_SERVER['DOCUMENT_ROOT'] = $composerConfig['extra']['document-root']; |
||
| 55 | return $_SERVER['DOCUMENT_ROOT']; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | return (string) $_SERVER['DOCUMENT_ROOT']; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $channel |
||
| 64 | * @return false|LoggerInterface |
||
| 65 | */ |
||
| 66 | public static function getLogger($channel) { |
||
| 67 | |||
| 68 | if(isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) { |
||
| 69 | $logClass = $_ENV['APP_LOG_CLASS']; |
||
| 70 | $log = new $logClass($channel); |
||
| 71 | if($log instanceof LoggerInterface) { |
||
| 72 | return $log; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return mixed|string |
||
| 81 | */ |
||
| 82 | public static function getCrontabFile() { |
||
| 83 | |||
| 84 | if(isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) { |
||
| 85 | return rtrim($_ENV['BX_CRONTAB_FOLDER'], "/") . '/bx_crontab.json'; |
||
| 86 | } |
||
| 87 | |||
| 88 | return self::getDocumentRoot() . self::CRON_TAB_FILE; |
||
| 89 | } |
||
| 90 | |||
| 91 | public static function getCrontabTimeout() { |
||
| 98 | } |
||
| 99 | |||
| 100 | public static function getBxCrontabPeriod() { |
||
| 101 | |||
| 102 | if(isset($_ENV['BX_CRONTAB_PERIOD']) && is_numeric($_ENV['BX_CRONTAB_PERIOD'])) { |
||
| 103 | return (int) $_ENV['BX_CRONTAB_PERIOD']; |
||
| 104 | } |
||
| 105 | |||
| 106 | return self::BX_CRONTAB_PERIOD; |
||
| 107 | } |
||
| 108 | |||
| 109 | public static function getSwitch($name, $state) { |
||
| 110 | |||
| 111 | if(isset($_ENV[$name])) { |
||
| 112 | |||
| 113 | $val = strtolower(trim($_ENV[$name])); |
||
| 114 | if($state == self::SWITCH_STATE_ON) { |
||
| 115 | if($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') { |
||
| 116 | return true; |
||
| 117 | } |
||
| 118 | } else if($state == self::SWITCH_STATE_OFF) { |
||
| 119 | if($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') { |
||
| 120 | return true; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | public static function timeZoneSet() { |
||
| 129 | |||
| 130 | $timeZone = self::BX_CRONTAB_TIMEZONE; |
||
| 131 | |||
| 132 | if(isset($_ENV['BX_CRONTAB_TIMEZONE']) && $_ENV['BX_CRONTAB_TIMEZONE']) { |
||
| 133 | $timeZone = trim($_ENV['BX_CRONTAB_TIMEZONE']); |
||
| 134 | } |
||
| 135 | |||
| 136 | date_default_timezone_set($timeZone); |
||
| 137 | } |
||
| 138 | |||
| 139 | public static function checkSleepInterval() { |
||
| 159 | } |
||
| 160 | } |