Complex classes like AuditController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AuditController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class AuditController extends Controller |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var bool True to cleanup the AuditEntry. |
||
| 26 | */ |
||
| 27 | public $entry; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool True to cleanup solo AuditEntry records (no trail/mail/error/javascript). |
||
| 31 | */ |
||
| 32 | public $entrySolo; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string|null Comma separated list of panels to cleanup. |
||
| 36 | */ |
||
| 37 | public $panels; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int|null Max age in days to cleanup, if null then the panel settings are used. |
||
| 41 | */ |
||
| 42 | public $age; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @inheritdoc |
||
| 46 | */ |
||
| 47 | public function options($actionID) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Cleanup the Audit data |
||
| 57 | * |
||
| 58 | * @return int|void |
||
| 59 | */ |
||
| 60 | public function actionCleanup() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Displays a summary of the data and dates to clean |
||
| 104 | * |
||
| 105 | * @param bool $entry |
||
| 106 | * @param bool $entrySolo |
||
| 107 | * @param array $panels |
||
| 108 | * @param int|null $maxAge |
||
| 109 | */ |
||
| 110 | protected function preCleanupSummary($entry, $entrySolo, $panels, $maxAge) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Cleans the AuditEntry data |
||
| 151 | * |
||
| 152 | * @param $maxAge |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | protected function cleanupEntry($maxAge) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Cleans the AuditEntry solo data (no trail/mail/error/javascript) |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | protected function cleanupEntrySolo() |
||
| 182 | { |
||
| 183 | $this->stdout("\n*** cleaning AuditEntry solo", Console::FG_YELLOW); |
||
| 184 | $start = microtime(true); |
||
| 185 | $count = 0; |
||
| 186 | foreach (AuditEntry::find()->each(100) as $auditEntry) { |
||
| 187 | /** @var AuditEntry $auditEntry */ |
||
| 188 | /** @var Audit $audit */ |
||
| 189 | $audit = Yii::$app->getModule('audit'); |
||
| 190 | $auditEntryCurrent = $audit->getEntry(); |
||
| 191 | if ($auditEntryCurrent && $auditEntryCurrent->id == $auditEntry->id) { |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | if (!$auditEntry->hasRelatedData()) { |
||
| 195 | foreach ($auditEntry->data as $data) { |
||
| 196 | $data->delete(); |
||
| 197 | } |
||
| 198 | try { |
||
| 199 | $auditEntry->delete(); |
||
| 200 | $count++; |
||
| 201 | $this->stdout('.', Console::FG_CYAN); |
||
| 202 | } catch (Exception $e) { |
||
|
|
|||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | $time = microtime(true) - $start; |
||
| 207 | $this->stdout("\n*** cleaned AuditEntry (records: " . $count . ",time: " . sprintf("%.3f", $time) . "s)\n", Console::FG_GREEN); |
||
| 208 | return true; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Cleans the Panel data |
||
| 213 | * |
||
| 214 | * @param $id |
||
| 215 | * @param $maxAge |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | protected function cleanupPanel($id, $maxAge) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Email errors to support email. |
||
| 242 | * |
||
| 243 | * @param string|null $email |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function actionErrorEmail($email = null) |
||
| 289 | |||
| 290 | } |
||
| 291 |