| Total Complexity | 40 |
| Total Lines | 337 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like QueryFacade 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 QueryFacade, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class QueryFacade extends AbstractFacade |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | * read => edit action for single row insert, update or delete |
||
| 21 | * save => save action for insert, update or delete |
||
| 22 | * select => edit action for bulk update |
||
| 23 | * clone => clone a selected set of data rows |
||
| 24 | */ |
||
| 25 | private string $action; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private string $operation; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @return DataRowWriter |
||
| 34 | */ |
||
| 35 | private function writer(): DataRowWriter |
||
| 36 | { |
||
| 37 | $fieldValue = new DataFieldValue($this->page, $this->driver, |
||
|
|
|||
| 38 | $this->utils, $this->action, $this->operation); |
||
| 39 | $fieldInput = new DataFieldInput($this->page, $this->driver, |
||
| 40 | $this->utils, $this->action, $this->operation); |
||
| 41 | return new DataRowWriter($this->page, $this->driver, $this->utils, |
||
| 42 | $this->action, $this->operation, $fieldValue, $fieldInput); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return DataRowReader |
||
| 47 | */ |
||
| 48 | private function reader(): DataRowReader |
||
| 49 | { |
||
| 50 | return new DataRowReader($this->page, $this->driver, $this->utils); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get the table fields |
||
| 55 | * |
||
| 56 | * @param string $table The table name |
||
| 57 | * @param array $options The query options |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | private function getFields(string $table, array $options): array |
||
| 62 | { |
||
| 63 | // From edit.inc.php |
||
| 64 | $fields = $this->driver->fields($table); |
||
| 65 | // Important: get the where clauses before filtering the fields. |
||
| 66 | $where = $this->operation === 'insert' ? [] : |
||
| 67 | $this->driver->where($options, $fields); |
||
| 68 | // Remove fields without the required privilege, or that cannot be edited. |
||
| 69 | $fields = array_filter($fields, fn(TableFieldEntity $field) => |
||
| 70 | isset($field->privileges[$this->operation]) && |
||
| 71 | $this->page->fieldName($field) !== '' && !$field->generated); |
||
| 72 | |||
| 73 | return [$fields, $where]; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get data for insert in a table |
||
| 78 | * |
||
| 79 | * @param string $table The table name |
||
| 80 | * @param array $options The query options |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | public function getInsertData(string $table, array $options = []): array |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param array<TableFieldEntity> $fields |
||
| 104 | * |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | private function getRowSelectClauses(array $fields): array |
||
| 108 | { |
||
| 109 | // if (!$this->driver->support("table")) { |
||
| 110 | // return ["*"]; |
||
| 111 | // } |
||
| 112 | |||
| 113 | // From edit.inc.php |
||
| 114 | $select = []; |
||
| 115 | foreach ($fields as $name => $field) { |
||
| 116 | if (isset($field->privileges["select"])) { |
||
| 117 | $as = $this->action === 'clone' && $field->autoIncrement ? "''" : |
||
| 118 | $this->driver->convertField($field); |
||
| 119 | $select[] = ($as ? "$as AS " : "") . $this->driver->escapeId($name); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | return $select; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get data for update/delete of a single row. |
||
| 127 | * |
||
| 128 | * @param string $table The table name |
||
| 129 | * @param array $options The query options |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | public function getUpdateData(string $table, array $options = []): array |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Build the SQL query to insert a new item in a table |
||
| 178 | * |
||
| 179 | * @param string $table The table name |
||
| 180 | * @param array $options The query options |
||
| 181 | * @param array $values The updated values |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public function getInsertQuery(string $table, array $options, array $values): array |
||
| 186 | { |
||
| 187 | $this->action = 'save'; |
||
| 188 | $this->operation = 'insert'; |
||
| 189 | |||
| 190 | [$fields,] = $this->getFields($table, $options); |
||
| 191 | $values = $this->reader()->getInputValues($fields, $values); |
||
| 192 | |||
| 193 | $query = $this->driver->getInsertQuery($table, $values); |
||
| 194 | return $query !== '' ? ['query' => $query] : [ |
||
| 195 | 'error' => $this->utils->trans->lang('Unable to build the SQL code for this insert query.'), |
||
| 196 | ]; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Insert a new item in a table |
||
| 201 | * |
||
| 202 | * @param string $table The table name |
||
| 203 | * @param array $options The query options |
||
| 204 | * @param array $values The updated values |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function insertItem(string $table, array $options, array $values): array |
||
| 209 | { |
||
| 210 | $this->action = 'save'; |
||
| 211 | $this->operation = 'insert'; |
||
| 212 | |||
| 213 | [$fields,] = $this->getFields($table, $options); |
||
| 214 | $values = $this->reader()->getInputValues($fields, $values); |
||
| 215 | |||
| 216 | if (!$this->driver->insert($table, $values)) { |
||
| 217 | return [ |
||
| 218 | 'error' => $this->driver->error(), |
||
| 219 | ]; |
||
| 220 | } |
||
| 221 | |||
| 222 | $lastId = $this->driver->lastAutoIncrementId(); |
||
| 223 | return [ |
||
| 224 | 'message' => $this->utils->trans->lang('Item%s has been inserted.', |
||
| 225 | $lastId ? " $lastId" : ''), |
||
| 226 | ]; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $table |
||
| 231 | * @param array $options |
||
| 232 | * |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | private function getQueryLimit(string $table, array $options): int |
||
| 236 | { |
||
| 237 | // From edit.inc.php |
||
| 238 | $indexes = $this->driver->indexes($table); |
||
| 239 | $uniqueIds = $this->utils->uniqueIds($options['where'], $indexes); |
||
| 240 | return count($uniqueIds ?? []) === 0 ? 1 : 0; // Limit to 1 if no unique ids are found. |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Build the SQL query to update one or more items in a table |
||
| 245 | * |
||
| 246 | * @param string $table The table name |
||
| 247 | * @param array $options The query options |
||
| 248 | * @param array $values The updated values |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public function getUpdateQuery(string $table, array $options, array $values): array |
||
| 264 | ]; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Update one or more items in a table |
||
| 269 | * |
||
| 270 | * @param string $table The table name |
||
| 271 | * @param array $options The query options |
||
| 272 | * @param array $values The updated values |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function updateItem(string $table, array $options, array $values): array |
||
| 277 | { |
||
| 278 | $this->action = 'save'; |
||
| 279 | $this->operation = 'update'; |
||
| 280 | |||
| 281 | [$fields, $where] = $this->getFields($table, $options); |
||
| 282 | $values = $this->reader()->getInputValues($fields, $values); |
||
| 283 | $limit = $this->getQueryLimit($table, $options); |
||
| 284 | |||
| 285 | if (!$this->driver->update($table, $values, "\nWHERE $where", $limit)) { |
||
| 286 | return [ |
||
| 287 | 'error' => $this->driver->error(), |
||
| 288 | ]; |
||
| 289 | } |
||
| 290 | |||
| 291 | // Get the modified data |
||
| 292 | // Todo: check if the values in the where clause are changed. |
||
| 293 | $statement = $this->driver->select($table, array_keys($values), [$where]); |
||
| 294 | $result = !$statement ? null : $statement->fetchAssoc(); |
||
| 295 | if (!$result) { |
||
| 296 | return [ |
||
| 297 | 'warning' => $this->utils->trans->lang('Unable to read the updated row.'), |
||
| 298 | ]; |
||
| 299 | } |
||
| 300 | |||
| 301 | return [ |
||
| 302 | 'cols' => $this->writer()->getUpdatedRow($result, $fields, $options), |
||
| 303 | 'message' => $this->utils->trans->lang('Item has been updated.'), |
||
| 304 | ]; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Build the SQL query to delete one or more items in a table |
||
| 309 | * |
||
| 310 | * @param string $table The table name |
||
| 311 | * @param array $options The query options |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public function getDeleteQuery(string $table, array $options): array |
||
| 326 | ]; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Delete one or more items in a table |
||
| 331 | * |
||
| 332 | * @param string $table The table name |
||
| 333 | * @param array $options The query options |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | public function deleteItem(string $table, array $options): array |
||
| 353 | ]; |
||
| 354 | } |
||
| 356 |