| Conditions | 14 |
| Paths | 34 |
| Total Lines | 104 |
| Code Lines | 51 |
| 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 |
||
| 92 | function subtasksHandler($taskId, $taskArguments, $phpBinaryPath): void |
||
| 93 | { |
||
| 94 | // Check if subtasks are still running |
||
| 95 | // This in order to prevent the script from running multiple times on same objects |
||
| 96 | while (DB::queryFirstField( |
||
| 97 | 'SELECT COUNT(*) FROM ' . prefixTable('background_subtasks') . ' |
||
| 98 | WHERE is_in_progress = 1' |
||
| 99 | ) > 0) { |
||
| 100 | sleep(10); // Wait 10 seconds before continuing |
||
| 101 | } |
||
| 102 | // Are server processes still running? |
||
| 103 | serverProcessesHandler(); |
||
| 104 | |||
| 105 | // Get list of subtasks to be performed for this task id |
||
| 106 | $subTasks = getSubTasks($taskId); |
||
| 107 | $taskArgumentsArray = json_decode($taskArguments, true); |
||
| 108 | |||
| 109 | if (count($subTasks) === 0) { |
||
| 110 | // No subtasks to perform |
||
| 111 | // Task is finished |
||
| 112 | markTaskAsFinished($taskId, $taskArgumentsArray['new_user_id']); |
||
| 113 | |||
| 114 | // Clear all subtasks |
||
| 115 | DB::delete(prefixTable('background_subtasks'), 'task_id=%i', $taskId); |
||
| 116 | |||
| 117 | // Exit |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | |||
| 121 | foreach ($subTasks as $subTask) { |
||
| 122 | // Launch the process for this subtask |
||
| 123 | |||
| 124 | // Do we have already 5 process running? |
||
| 125 | if (countActiveSymfonyProcesses() <= 2) { |
||
| 126 | // Extract the subtask parameters |
||
| 127 | $subTaskParams = json_decode($subTask['task'], true); |
||
| 128 | |||
| 129 | if (WIP === true) { |
||
| 130 | error_log('Subtask in progress: '.$subTask['increment_id']." (".$taskId.") - "./** @scrutinizer ignore-type */ print_r($subTaskParams,true)); |
||
| 131 | } |
||
| 132 | |||
| 133 | // Build all subtasks if first one |
||
| 134 | if ((int) $subTaskParams['index'] === 0) { |
||
| 135 | if ($subTaskParams['step'] === 'step20') { |
||
| 136 | // Get total number of items |
||
| 137 | DB::query( |
||
| 138 | 'SELECT * |
||
| 139 | FROM ' . prefixTable('items') . ' |
||
| 140 | '.(isset($taskArgumentsArray['only_personal_items']) === true && $taskArgumentsArray['only_personal_items'] === 1 ? 'WHERE perso = 1' : '') |
||
| 141 | ); |
||
| 142 | createAllSubTasks($subTaskParams['step'], DB::count(), $subTaskParams['nb'], $taskId); |
||
| 143 | |||
| 144 | } elseif ($subTaskParams['step'] === 'step30') { |
||
| 145 | // Get total number of items |
||
| 146 | DB::query( |
||
| 147 | 'SELECT * |
||
| 148 | FROM ' . prefixTable('log_items') . ' |
||
| 149 | WHERE raison LIKE "at_pw :%" AND encryption_type = "teampass_aes"' |
||
| 150 | ); |
||
| 151 | createAllSubTasks($subTaskParams['step'], DB::count(), $subTaskParams['nb'], $taskId); |
||
| 152 | |||
| 153 | } elseif ($subTaskParams['step'] === 'step40') { |
||
| 154 | // Get total number of items |
||
| 155 | DB::query( |
||
| 156 | 'SELECT * |
||
| 157 | FROM ' . prefixTable('categories_items') . ' |
||
| 158 | WHERE encryption_type = "teampass_aes"' |
||
| 159 | ); |
||
| 160 | createAllSubTasks($subTaskParams['step'], DB::count(), $subTaskParams['nb'], $taskId); |
||
| 161 | |||
| 162 | } elseif ($subTaskParams['step'] === 'step50') { |
||
| 163 | // Get total number of items |
||
| 164 | DB::query( |
||
| 165 | 'SELECT * |
||
| 166 | FROM ' . prefixTable('suggestion') |
||
| 167 | ); |
||
| 168 | createAllSubTasks($subTaskParams['step'], DB::count(), $subTaskParams['nb'], $taskId); |
||
| 169 | |||
| 170 | } elseif ($subTaskParams['step'] === 'step60') { |
||
| 171 | // Get total number of items |
||
| 172 | DB::query( |
||
| 173 | 'SELECT * |
||
| 174 | FROM ' . prefixTable('files') . ' AS f |
||
| 175 | INNER JOIN ' . prefixTable('items') . ' AS i ON i.id = f.id_item |
||
| 176 | WHERE f.status = "' . TP_ENCRYPTION_NAME . '"' |
||
| 177 | ); |
||
| 178 | createAllSubTasks($subTaskParams['step'], DB::count(), $subTaskParams['nb'], $taskId); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | // Launch the subtask with the current parameters |
||
| 183 | $process = new Process([$phpBinaryPath, __DIR__.'/background_tasks___userKeysCreation_subtaskHdl.php', $subTask['increment_id'], $subTaskParams['index'], $subTaskParams['nb'], $subTaskParams['step'], $taskArguments, $taskId]); |
||
| 184 | $process->start(); |
||
| 185 | $pid = $process->getPid(); |
||
| 186 | |||
| 187 | // Update the subtask with the process id |
||
| 188 | updateSubTask($subTask['increment_id'], ['pid' => $pid]); |
||
| 189 | updateTask($taskId); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | sleep(10); // Wait 10 seconds before continuing |
||
| 193 | |||
| 194 | // Recursively call this function until all subtasks are finished |
||
| 195 | subtasksHandler($taskId, $taskArguments, $phpBinaryPath); |
||
| 196 | } |
||
| 358 | } |