| Conditions | 6 |
| Paths | 16 |
| Total Lines | 69 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 130 | public function archive($alert, $tables, OutputInterface $output) |
||
| 131 | { |
||
| 132 | $output->writeln("Processing Alert #{$alert['ResultID']}"); |
||
| 133 | |||
| 134 | $this->dbArchive->beginTransaction(); |
||
| 135 | |||
| 136 | // Get all data and insert it into the archive DB |
||
| 137 | foreach ($tables as $table) { |
||
| 138 | $output->writeln("Alert #{$alert['ResultID']} - Table: {$table}"); |
||
| 139 | |||
| 140 | $sql = "SELECT * FROM {$table} WHERE resultID = :result"; |
||
| 141 | |||
| 142 | $stm = $this->db->prepare($sql); |
||
| 143 | $stm->bindParam(':result', $alert['ResultID']); |
||
| 144 | $stm->execute(); |
||
| 145 | |||
| 146 | if ($stm->rowCount() > 0) { |
||
| 147 | $values = ''; |
||
| 148 | $cols = ''; |
||
| 149 | |||
| 150 | // Build the values so we can do all of this in one huge query, |
||
| 151 | // which helps with transmission over the internet greatly. |
||
| 152 | while ($row = $stm->fetch(\PDO::FETCH_ASSOC)) { |
||
| 153 | $cols = $this->buildCols($row); |
||
| 154 | $data = $this->buildValues($row); |
||
| 155 | $values .= "('{$data}'),"; |
||
| 156 | } |
||
| 157 | |||
| 158 | $values = rtrim($values, ','); |
||
| 159 | $sql = "INSERT INTO {$table} ({$cols}) VALUES {$values}"; |
||
| 160 | |||
| 161 | $this->dbArchive->exec($sql); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | try { |
||
| 166 | $output->writeln('Committing...'); |
||
| 167 | $this->dbArchive->commit(); |
||
| 168 | } catch (\Exception $e) { |
||
| 169 | $this->dbArchive->rollBack(); |
||
| 170 | throw new \Exception($e->getMessage()); |
||
| 171 | } |
||
| 172 | |||
| 173 | $records = 0; |
||
| 174 | |||
| 175 | $this->db->beginTransaction(); |
||
| 176 | |||
| 177 | // Loop through all tables and delete the alert's data from the DB |
||
| 178 | foreach ($tables as $table) { |
||
| 179 | $sql = "DELETE FROM {$table} WHERE resultID = :result"; |
||
| 180 | $stm = $this->db->prepare($sql); |
||
| 181 | $stm->execute(['result' => $alert['ResultID']]); |
||
| 182 | |||
| 183 | $this->recordsArchived += $stm->rowCount(); |
||
| 184 | $records += $stm->rowCount(); |
||
| 185 | |||
| 186 | $output->writeln("Archived {$stm->rowCount()} from Alert #{$alert['ResultID']} - Table {$table}"); |
||
| 187 | } |
||
| 188 | |||
| 189 | $this->db->commit(); |
||
| 190 | |||
| 191 | $output->writeln("{$records} records archived for Alert #{$alert['ResultID']}"); |
||
| 192 | $this->alertsArchived++; |
||
| 193 | |||
| 194 | // Set the alert as archived in the resultset |
||
| 195 | $sql = "UPDATE ws_results SET Archived = '1' WHERE ResultID = :result"; |
||
| 196 | $stm = $this->db->prepare($sql); |
||
| 197 | $stm->execute(['result' => $alert['ResultID']]); |
||
| 198 | } |
||
| 199 | |||
| 235 |