| Conditions | 9 |
| Paths | 9 |
| Total Lines | 132 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 186 | function handleTask(int $processId, array $ProcessArguments, array $SETTINGS, int $itemId = null): bool |
||
| 187 | { |
||
| 188 | provideLog('[PROCESS][#'. $processId.'][START]', $SETTINGS); |
||
| 189 | $task_to_perform = DB::queryFirstRow( |
||
| 190 | 'SELECT * |
||
| 191 | FROM ' . prefixTable('background_subtasks') . ' |
||
| 192 | WHERE task_id = %i AND finished_at IS NULL |
||
| 193 | ORDER BY increment_id ASC', |
||
| 194 | $processId |
||
| 195 | ); |
||
| 196 | |||
| 197 | // get the process object |
||
| 198 | //$processObject = json_decode($ProcessArguments['object_key'], true); |
||
| 199 | |||
| 200 | if (DB::count() > 0) { |
||
| 201 | // check if a linux process is not currently on going |
||
| 202 | // if sub_task_in_progress === 1 then exit |
||
| 203 | if ((int) $task_to_perform['sub_task_in_progress'] !== 0) { |
||
| 204 | // Task is currently being in progress by another server process |
||
| 205 | provideLog('[TASK][#'. $task_to_perform['increment_id'].'][WARNING] Similar task already being processes', $SETTINGS); |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | // handle next task |
||
| 210 | $args = json_decode($task_to_perform['task'], true); |
||
| 211 | provideLog('[TASK][#'. $task_to_perform['increment_id'].'][START]Task '.$args['step'], $SETTINGS); |
||
| 212 | |||
| 213 | // flag as in progress |
||
| 214 | DB::update( |
||
| 215 | prefixTable('background_tasks'), |
||
| 216 | array( |
||
| 217 | 'updated_at' => time(), |
||
| 218 | 'is_in_progress' => 1, |
||
| 219 | ), |
||
| 220 | 'increment_id = %i', |
||
| 221 | $processId |
||
| 222 | ); |
||
| 223 | |||
| 224 | // flag task as on going |
||
| 225 | if ((int) $args['index'] === 0) { |
||
| 226 | DB::update( |
||
| 227 | prefixTable('background_subtasks'), |
||
| 228 | array( |
||
| 229 | 'is_in_progress' => 1, |
||
| 230 | ), |
||
| 231 | 'increment_id = %i', |
||
| 232 | $task_to_perform['increment_id'] |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | |||
| 236 | // flag sub task in progress as on going |
||
| 237 | DB::update( |
||
| 238 | prefixTable('background_subtasks'), |
||
| 239 | array( |
||
| 240 | 'sub_task_in_progress' => 1, |
||
| 241 | ), |
||
| 242 | 'increment_id = %i', |
||
| 243 | $task_to_perform['increment_id'] |
||
| 244 | ); |
||
| 245 | |||
| 246 | // handle the task step |
||
| 247 | handleTaskStep($args, $ProcessArguments, $SETTINGS); |
||
| 248 | |||
| 249 | // update the task status |
||
| 250 | DB::update( |
||
| 251 | prefixTable('background_subtasks'), |
||
| 252 | array( |
||
| 253 | 'sub_task_in_progress' => 0, // flag sub task is no more in prgoress |
||
| 254 | 'task' => json_encode(["status" => "Done"]), |
||
| 255 | 'is_in_progress' => -1, |
||
| 256 | 'finished_at' => time(), |
||
| 257 | 'updated_at' => time(), |
||
| 258 | ), |
||
| 259 | 'increment_id = %i', |
||
| 260 | $task_to_perform['increment_id'] |
||
| 261 | ); |
||
| 262 | |||
| 263 | provideLog('[TASK]['.$args['step'].'] starting at '.$args['index'].' is done.', $SETTINGS); |
||
| 264 | |||
| 265 | // are all tasks done? |
||
| 266 | DB::query( |
||
| 267 | 'SELECT * |
||
| 268 | FROM ' . prefixTable('background_subtasks') . ' |
||
| 269 | WHERE task_id = %i AND finished_at IS NULL', |
||
| 270 | $processId |
||
| 271 | ); |
||
| 272 | if (DB::count() === 0) { |
||
| 273 | // all tasks are done |
||
| 274 | provideLog('[PROCESS]['.$processId.'][FINISHED]', $SETTINGS); |
||
| 275 | DB::debugmode(false); |
||
| 276 | DB::update( |
||
| 277 | prefixTable('background_tasks'), |
||
| 278 | array( |
||
| 279 | 'finished_at' => time(), |
||
| 280 | 'is_in_progress' => -1, |
||
| 281 | 'arguments' => json_encode([ |
||
| 282 | 'new_user_id' => isset($ProcessArguments['new_user_id']) === true ? $ProcessArguments['new_user_id'] : '', |
||
| 283 | ]) |
||
| 284 | ), |
||
| 285 | 'increment_id = %i', |
||
| 286 | $processId |
||
| 287 | ); |
||
| 288 | |||
| 289 | // if item was being updated then remove the edition lock |
||
| 290 | if (is_null($itemId) === false) { |
||
| 291 | DB::delete(prefixTable('items_edition'), 'item_id = %i', $itemId); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | return false; |
||
| 295 | |||
| 296 | } else { |
||
| 297 | // no more task to perform |
||
| 298 | provideLog('[PROCESS]['.$processId.'][FINISHED]', $SETTINGS); |
||
| 299 | DB::update( |
||
| 300 | prefixTable('background_tasks'), |
||
| 301 | array( |
||
| 302 | 'finished_at' => time(), |
||
| 303 | 'is_in_progress' => -1, |
||
| 304 | 'arguments' => json_encode([ |
||
| 305 | 'item_id' => isset($ProcessArguments['item_id']) === true ? $ProcessArguments['item_id'] : '', |
||
| 306 | ]) |
||
| 307 | ), |
||
| 308 | 'increment_id = %i', |
||
| 309 | $processId |
||
| 310 | ); |
||
| 311 | |||
| 312 | // if item was being updated then remove the edition lock |
||
| 313 | if (is_null($itemId) === false) { |
||
| 314 | DB::delete(prefixTable('items_edition'), 'item_id = %i', $itemId); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | return false; |
||
| 318 | } |
||
| 411 |