| Total Complexity | 42 |
| Total Lines | 278 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like System 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 System, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class System extends Base |
||
| 18 | { |
||
| 19 | /** {@inheritdoc} */ |
||
| 20 | public $moduleName = 'System'; |
||
| 21 | |||
| 22 | /** @var string[] Methods list */ |
||
| 23 | public $methods = [ |
||
| 24 | 'history' => 'History of uploaded updates', |
||
| 25 | 'update' => 'Update', |
||
| 26 | 'checkRegStatus' => 'Check registration status', |
||
| 27 | 'deleteRegistration' => 'Delete registration data', |
||
| 28 | 'showProducts' => 'Show active products', |
||
| 29 | 'reloadModule' => 'Reload modules', |
||
| 30 | 'reloadUserPrivileges' => 'Reload users privileges', |
||
| 31 | 'reloadMenus' => 'Reload menus', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * History of uploaded updates. |
||
| 36 | * |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | public function history(): void |
||
| 40 | { |
||
| 41 | $table = array_map(function ($item) { |
||
| 42 | $item['result'] = $item['result'] ? 'OK' : 'Error'; |
||
| 43 | unset($item['id']); |
||
| 44 | return $item; |
||
| 45 | }, \Settings_Updates_Module_Model::getUpdates()); |
||
| 46 | if ($table) { |
||
|
|
|||
| 47 | $this->climate->table($table); |
||
| 48 | } else { |
||
| 49 | $this->climate->lightGreen('No updates'); |
||
| 50 | } |
||
| 51 | if (!$this->climate->arguments->defined('action')) { |
||
| 52 | $this->cli->actionsList('System'); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Update CRM. |
||
| 58 | * |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | public function update(): void |
||
| 62 | { |
||
| 63 | $maxExecutionTime = ini_get('max_execution_time'); |
||
| 64 | if ($maxExecutionTime < 1 || $maxExecutionTime > 600) { |
||
| 65 | $this->climate->lightGreen('Max execution time = ' . $maxExecutionTime); |
||
| 66 | } else { |
||
| 67 | $this->climate->lightRed('Max execution time = ' . $maxExecutionTime); |
||
| 68 | } |
||
| 69 | $this->climate->arguments->add([ |
||
| 70 | 'type' => [ |
||
| 71 | 'prefix' => 't', |
||
| 72 | 'description' => 'Update type. Values: patches, version', |
||
| 73 | ], |
||
| 74 | ]); |
||
| 75 | if ($this->helpMode) { |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | $this->climate->arguments->parse(); |
||
| 79 | if ($this->climate->arguments->defined('type')) { |
||
| 80 | $this->updateByType($this->climate->arguments->get('type')); |
||
| 81 | } else { |
||
| 82 | $this->updateBySelect(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Update by package type. |
||
| 88 | * |
||
| 89 | * @param string $type Package type. Values: patches, version |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | private function updateByType(string $type): void |
||
| 94 | { |
||
| 95 | $types = ['patches', 'version']; |
||
| 96 | if (!\in_array($this->climate->arguments->get('type'), $types)) { |
||
| 97 | $this->climate->white('Type not found. Allowed types:')->columns($types); |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | $versionUpdate = 'version' === $type; |
||
| 101 | foreach (\App\YetiForce\Updater::getToInstall() as $package) { |
||
| 102 | $versionCompare = $package['fromVersion'] !== $package['toVersion']; |
||
| 103 | if (($versionCompare && !$versionUpdate) || (!$versionCompare && $versionUpdate)) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | if (!\App\YetiForce\Updater::isDownloaded($package)) { |
||
| 107 | $this->climate->inline($package['label'] . ' - Downloading a package ...'); |
||
| 108 | \App\YetiForce\Updater::download($package); |
||
| 109 | $this->climate->out('- downloaded'); |
||
| 110 | } |
||
| 111 | $this->updateByPackage($package); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Update by selecting a package. |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | private function updateBySelect(): void |
||
| 121 | { |
||
| 122 | $options = []; |
||
| 123 | $toInstall = \App\YetiForce\Updater::getToInstall(); |
||
| 124 | foreach ($toInstall as $package) { |
||
| 125 | $option = "{$package['label']}"; |
||
| 126 | if ($package['fromVersion'] !== $package['toVersion']) { |
||
| 127 | $option .= " ({$package['fromVersion']} >> {$package['toVersion']})"; |
||
| 128 | } |
||
| 129 | if (\App\YetiForce\Updater::isDownloaded($package)) { |
||
| 130 | $option .= ' - Downloaded, ready to install'; |
||
| 131 | } else { |
||
| 132 | $option .= ' - To download'; |
||
| 133 | } |
||
| 134 | $options[$package['hash']] = $option; |
||
| 135 | } |
||
| 136 | if (!$options) { |
||
| 137 | $this->climate->lightBlue('No updates available'); |
||
| 138 | return; |
||
| 139 | } |
||
| 140 | $input = $this->climate->radio('Updates available:', $options); |
||
| 141 | $hash = $input->prompt(); |
||
| 142 | foreach ($toInstall as $package) { |
||
| 143 | if ($package['hash'] === $hash) { |
||
| 144 | if (\App\YetiForce\Updater::isDownloaded($package)) { |
||
| 145 | $this->updateByPackage($package); |
||
| 146 | } else { |
||
| 147 | \App\YetiForce\Updater::download($package); |
||
| 148 | $this->update(); |
||
| 149 | } |
||
| 150 | return; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Update package. |
||
| 157 | * |
||
| 158 | * @param array $package |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | private function updateByPackage(array $package): void |
||
| 163 | { |
||
| 164 | $startTime = microtime(true); |
||
| 165 | try { |
||
| 166 | $packageInstance = new \vtlib\Package(); |
||
| 167 | $this->climate->white($package['label'] . ' - Installing the package'); |
||
| 168 | $path = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . \Settings_ModuleManager_Module_Model::getUploadDirectory() . \DIRECTORY_SEPARATOR . $package['hash'] . '.zip'; |
||
| 169 | $response = $packageInstance->import($path, true); |
||
| 170 | if ($packageInstance->_errorText) { |
||
| 171 | $this->climate->lightRed($packageInstance->_errorText); |
||
| 172 | } else { |
||
| 173 | echo $response . PHP_EOL; |
||
| 174 | unlink($path); |
||
| 175 | } |
||
| 176 | } catch (\Throwable $th) { |
||
| 177 | $this->climate->lightRed($th->__toString()); |
||
| 178 | } |
||
| 179 | $this->climate->lightBlue('Total time: ' . round(microtime(true) - $startTime, 2)); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check registration status. |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | public function checkRegStatus(): void |
||
| 188 | { |
||
| 189 | \App\YetiForce\Register::check(true); |
||
| 190 | $this->climate->bold('Status: ' . \App\Language::translate(\App\YetiForce\Register::STATUS_MESSAGES[\App\YetiForce\Register::getStatus()], 'Settings::Companies')); |
||
| 191 | $this->climate->border('─', 200); |
||
| 192 | $this->climate->bold('APP ID: ' . \App\YetiForce\Register::getInstanceKey()); |
||
| 193 | $this->climate->border('─', 200); |
||
| 194 | $this->climate->bold('CRM ID: ' . \App\YetiForce\Register::getCrmKey()); |
||
| 195 | $this->climate->border('─', 200); |
||
| 196 | $this->climate->bold('Provider: ' . \App\YetiForce\Register::getProvider()); |
||
| 197 | $this->climate->border('─', 200); |
||
| 198 | $table = []; |
||
| 199 | foreach (\App\Company::getAll() as $row) { |
||
| 200 | $table[] = [ |
||
| 201 | 'name' => $row['name'], |
||
| 202 | 'status' => $row['status'], |
||
| 203 | 'type' => $row['type'], |
||
| 204 | 'companysize' => $row['companysize'], |
||
| 205 | 'vat_id' => $row['vat_id'], |
||
| 206 | ]; |
||
| 207 | } |
||
| 208 | $this->climate->table($table); |
||
| 209 | $this->climate->border('─', 200); |
||
| 210 | if (!$this->climate->arguments->defined('action')) { |
||
| 211 | $this->cli->actionsList('System'); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Show active products. |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function showProducts(): void |
||
| 221 | { |
||
| 222 | $table = []; |
||
| 223 | foreach (\App\YetiForce\Register::getProducts() as $row) { |
||
| 224 | $row['params'] = \App\Utils::varExport($row['params']); |
||
| 225 | $table[] = $row; |
||
| 226 | } |
||
| 227 | $table ? $this->climate->table($table) : $this->climate->bold('None'); |
||
| 228 | $this->climate->border('─', 200); |
||
| 229 | if (!$this->climate->arguments->defined('action')) { |
||
| 230 | $this->cli->actionsList('System'); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Reload modules. |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | public function reloadModule(): void |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Reload users privileges. |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | public function reloadUserPrivileges(): void |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Delete registration data. |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | public function deleteRegistration(): void |
||
| 275 | { |
||
| 276 | \App\Db::getInstance('admin')->createCommand()->update('s_#__companies', [ |
||
| 277 | 'status' => 0, |
||
| 278 | 'name' => '', 'industry' => '', 'vat_id' => '', 'city' => '', 'address' => '', |
||
| 279 | 'post_code' => '', 'country' => '', 'companysize' => '', 'website' => '', 'logo' => '', |
||
| 280 | 'firstname' => '', 'lastname' => '', 'email' => '', 'facebook' => '', 'twitter' => '', 'linkedin' => '', |
||
| 281 | ])->execute(); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Reload menus. |
||
| 286 | * |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | public function reloadMenus(): void |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } |
||
| 298 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.