| Total Complexity | 46 |
| Total Lines | 288 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like DockerEntrypoint 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 DockerEntrypoint, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class DockerEntrypoint extends Di\Injectable |
||
| 37 | { |
||
| 38 | private const PATH_DB = '/cf/conf/mikopbx.db'; |
||
| 39 | private const pathInc = '/etc/inc/mikopbx-settings.json'; |
||
| 40 | public float $workerStartTime; |
||
| 41 | private array $jsonSettings; |
||
| 42 | private array $settings; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor for the DockerEntrypoint class. |
||
| 46 | * Registers the shutdown handler and enables asynchronous signal handling. |
||
| 47 | */ |
||
| 48 | public function __construct() |
||
| 49 | { |
||
| 50 | pcntl_async_signals(true); |
||
| 51 | register_shutdown_function([$this, 'shutdownHandler']); |
||
| 52 | |||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Handles the shutdown event for the Docker container. |
||
| 57 | * Logs the time taken since the worker start and any last-minute errors. |
||
| 58 | */ |
||
| 59 | public function shutdownHandler(): void |
||
| 60 | { |
||
| 61 | $e = error_get_last(); |
||
| 62 | $delta = round(microtime(true) - $this->workerStartTime, 2); |
||
| 63 | if ($e === null) { |
||
| 64 | SystemMessages::sysLogMsg(static::class, "shutdownHandler after $delta seconds", LOG_DEBUG); |
||
| 65 | } else { |
||
| 66 | $details = (string)print_r($e, true); |
||
| 67 | SystemMessages::sysLogMsg(static::class, "shutdownHandler after $delta seconds with error: $details", LOG_DEBUG); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Initiates the startup sequence for the MikoPBX system. |
||
| 73 | * Processes include system log initialization, database preparation, settings retrieval and application, |
||
| 74 | * and triggering system startup routines. |
||
| 75 | */ |
||
| 76 | public function start(): void |
||
| 77 | { |
||
| 78 | $this->workerStartTime = microtime(true); |
||
| 79 | $syslogd = Util::which('syslogd'); |
||
| 80 | // Start the system log. |
||
| 81 | Processes::mwExecBg($syslogd . ' -S -C512'); |
||
| 82 | |||
| 83 | // Update WWW user id and group id. |
||
| 84 | $this->changeWwwUserID(); |
||
| 85 | |||
| 86 | // Prepare database |
||
| 87 | $this->prepareDatabase(); |
||
| 88 | |||
| 89 | // Get default settings |
||
| 90 | $this->getDefaultSettings(); |
||
| 91 | |||
| 92 | // Update DB values |
||
| 93 | $this->applyEnvironmentSettings(); |
||
| 94 | |||
| 95 | // Start the MikoPBX system. |
||
| 96 | $this->startTheMikoPBXSystem(); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Updates the system user 'www' with new user and group IDs if they are provided through environment variables |
||
| 101 | * or from existing configuration files. |
||
| 102 | */ |
||
| 103 | private function changeWwwUserID(): void |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Prepares the SQLite database for use, checking for table existence and restoring from defaults if necessary. |
||
| 162 | * @return array An array containing the results of the database check commands. |
||
| 163 | */ |
||
| 164 | public function prepareDatabase(): array |
||
| 165 | { |
||
| 166 | $sqlite3 = Util::which('sqlite3'); |
||
| 167 | $rm = Util::which('rm'); |
||
| 168 | $cp = Util::which('cp'); |
||
| 169 | $out = []; |
||
| 170 | Processes::mwExec("$sqlite3 " . self::PATH_DB . ' .tables', $out); |
||
| 171 | if (trim(implode('', $out)) === '') { |
||
| 172 | Util::mwMkdir(dirname(self::PATH_DB)); |
||
| 173 | Processes::mwExec("$rm -rf " . self::PATH_DB . "; $cp /conf.default/mikopbx.db " . self::PATH_DB); |
||
| 174 | Util::addRegularWWWRights(self::PATH_DB); |
||
| 175 | } |
||
| 176 | return array($rm, $out); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieves default settings from JSON configuration and the database, |
||
| 181 | * setting up initial configuration states required for system operations. |
||
| 182 | */ |
||
| 183 | private function getDefaultSettings(): void |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Applies configuration settings from environment variables to the system, |
||
| 212 | * updating both database and JSON stored settings as necessary. |
||
| 213 | */ |
||
| 214 | private function applyEnvironmentSettings(): void |
||
| 215 | { |
||
| 216 | $reflection = new ReflectionClass(PbxSettingsConstants::class); |
||
| 217 | $constants = $reflection->getConstants(); |
||
| 218 | |||
| 219 | foreach ($constants as $name => $dbKey) { |
||
| 220 | $envValue = getenv($name); |
||
| 221 | if ($envValue !== false) { |
||
| 222 | switch ($dbKey) { |
||
| 223 | case PbxSettingsConstants::BEANSTALK_PORT: |
||
| 224 | case PbxSettingsConstants::REDIS_PORT: |
||
| 225 | case PbxSettingsConstants::GNATS_PORT: |
||
| 226 | $this->updateJsonSettings($dbKey, 'port', intval($envValue)); |
||
| 227 | break; |
||
| 228 | case PbxSettingsConstants::GNATS_HTTP_PORT: |
||
| 229 | $this->updateJsonSettings('gnats', 'httpPort', intval($envValue)); |
||
| 230 | break; |
||
| 231 | case PbxSettingsConstants::ENABLE_USE_NAT: |
||
| 232 | if ($envValue==='1'){ |
||
| 233 | $this->reconfigureNetwork("topology", LanInterfaces::TOPOLOGY_PRIVATE); |
||
| 234 | } |
||
| 235 | break; |
||
| 236 | case PbxSettingsConstants::EXTERNAL_SIP_HOST_NAME: |
||
| 237 | $this->reconfigureNetwork("exthostname", $envValue); |
||
| 238 | break; |
||
| 239 | case PbxSettingsConstants::EXTERNAL_SIP_IP_ADDR: |
||
| 240 | $this->reconfigureNetwork("extipaddr", $envValue); |
||
| 241 | break; |
||
| 242 | default: |
||
| 243 | $this->updateDBSetting($dbKey, $envValue); |
||
| 244 | break; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Reconfigures a network setting in the database. |
||
| 252 | * |
||
| 253 | * This method updates the value of a specified setting for network interfaces |
||
| 254 | * that are marked as connected to the internet in the database. It logs the outcome |
||
| 255 | * of the operation, detailing success or failure along with the executed command |
||
| 256 | * if an error occurs. |
||
| 257 | * |
||
| 258 | * @param string $key The database column key representing the setting to update. |
||
| 259 | * @param string $newValue The new value to assign to the specified setting. |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | private function reconfigureNetwork(string $key, string $newValue): void |
||
| 263 | { |
||
| 264 | $sqlite3 = Util::which('sqlite3'); |
||
| 265 | $dbPath = self::PATH_DB; |
||
| 266 | $out = []; |
||
| 267 | $command = "$sqlite3 $dbPath \"UPDATE m_LanInterfaces SET $key='$newValue' WHERE internet='1'\""; |
||
| 268 | $res = Processes::mwExec($command, $out); |
||
| 269 | if ($res === 0) { |
||
| 270 | SystemMessages::sysLogMsg(__METHOD__, " - Update m_LanInterfaces.$key to '$newValue'", LOG_INFO); |
||
| 271 | } else { |
||
| 272 | SystemMessages::sysLogMsg(__METHOD__, " - Update m_LanInterfaces.$key to '$newValue' failed: " . implode($out) . PHP_EOL . 'Command:' . PHP_EOL . $command, LOG_ERR); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | /** |
||
| 276 | * Updates the specified setting in the JSON configuration file. |
||
| 277 | * @param string $path The JSON path where the setting is stored. |
||
| 278 | * @param string $key The setting key to update. |
||
| 279 | * @param mixed $newValue The new value to set. |
||
| 280 | */ |
||
| 281 | private function updateJsonSettings(string $path, string $key, $newValue): void |
||
| 282 | { |
||
| 283 | if ($this->jsonSettings[$path][$key] ?? null !== $newValue) |
||
| 284 | $this->jsonSettings[$path][$key] = $newValue; |
||
| 285 | $newData = json_encode($this->jsonSettings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
||
| 286 | file_put_contents(self::pathInc, $newData); |
||
| 287 | SystemMessages::sysLogMsg(__METHOD__, " - Update $path:$key to '$newValue' in /etc/inc/mikopbx-settings.json", LOG_INFO); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Updates a specified setting directly in the database. |
||
| 292 | * @param string $key The key of the setting to update. |
||
| 293 | * @param string $newValue The new value for the setting. |
||
| 294 | */ |
||
| 295 | private function updateDBSetting(string $key, string $newValue): void |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Executes the final commands to start the MikoPBX system, clearing temporary files and running system scripts. |
||
| 315 | */ |
||
| 316 | public function startTheMikoPBXSystem(): void |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 329 | $main->start(); |