Total Complexity | 60 |
Total Lines | 371 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CallQueuesController 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 CallQueuesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class CallQueuesController extends BaseController |
||
30 | { |
||
31 | /** |
||
32 | * Builds call queues representation |
||
33 | */ |
||
34 | public function indexAction(): void |
||
35 | { |
||
36 | $records = CallQueueMembers::find(); |
||
37 | $callQueueMembers = []; |
||
38 | foreach ($records as $record) { |
||
39 | $callQueueMembers[$record->queue][$record->id] = [ |
||
40 | 'priority' => $record->priority, |
||
41 | 'represent' => $record->Extensions === null ? 'ERROR' : $record->Extensions->getRepresent() |
||
42 | ]; |
||
43 | } |
||
44 | |||
45 | $records = CallQueues::find(); |
||
46 | $callQueuesList = []; |
||
47 | foreach ($records as $record) { |
||
48 | $members = $callQueueMembers[$record->uniqid]; |
||
49 | if (is_array($members)) { |
||
50 | usort($members, [__CLASS__, 'sortArrayByPriority']); |
||
51 | } else { |
||
52 | $members = []; |
||
53 | } |
||
54 | $callQueuesList[] = [ |
||
55 | 'uniqid' => $record->uniqid, |
||
56 | 'name' => $record->name, |
||
57 | 'extension' => $record->extension, |
||
58 | 'members' => $members, |
||
59 | 'description' => $record->description, |
||
60 | ]; |
||
61 | } |
||
62 | $this->view->callQueuesList = $callQueuesList; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Modify the call queue action. |
||
67 | * |
||
68 | * @param string $uniqid (optional) The identifier of the queue being modified. |
||
69 | * @return void |
||
70 | */ |
||
71 | public function modifyAction(string $uniqid = ''): void |
||
151 | } |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Save the queue via AJAX request from the form. |
||
156 | * |
||
157 | * This method saves the queue by processing an AJAX request from the form. |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | public function saveAction(): void |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Update the extension parameters. |
||
219 | * |
||
220 | * This method updates the parameters of the internal extension. |
||
221 | * |
||
222 | * @param \MikoPBX\Common\Models\Extensions $extension The extension object to update. |
||
223 | * @param array $data The array of fields from the POST request. |
||
224 | * |
||
225 | * @return bool The update result. Returns true if the update is successful, false otherwise. |
||
226 | */ |
||
227 | private function updateExtension(Extensions $extension, array $data): bool |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Update queue parameters. |
||
243 | * |
||
244 | * This method updates the parameters of a queue. |
||
245 | * |
||
246 | * @param \MikoPBX\Common\Models\CallQueues $queue The queue object to update. |
||
247 | * @param array $data The array of fields from the POST request. |
||
248 | * |
||
249 | * @return bool The update result. Returns true if the update is successful, false otherwise. |
||
250 | */ |
||
251 | private function updateQueue(CallQueues $queue, array $data): bool |
||
252 | { |
||
253 | foreach ($queue as $name => $value) { |
||
254 | switch ($name) { |
||
255 | case "extension": |
||
256 | case "name": |
||
257 | $queue->$name = $data[$name]; |
||
258 | break; |
||
259 | case "recive_calls_while_on_a_call": |
||
260 | case "announce_position": |
||
261 | case "announce_hold_time": |
||
262 | if (array_key_exists($name, $data)) { |
||
263 | $queue->$name = ($data[$name] == 'on') ? "1" : "0"; |
||
264 | } else { |
||
265 | $queue->$name = "0"; |
||
266 | } |
||
267 | break; |
||
268 | |||
269 | case "periodic_announce_sound_id": |
||
270 | case "moh_sound_id": |
||
271 | case "redirect_to_extension_if_repeat_exceeded": |
||
272 | case "redirect_to_extension_if_empty": |
||
273 | if (! array_key_exists($name, $data) || empty($data[$name])) { |
||
274 | $queue->$name = null; |
||
275 | continue 2; |
||
276 | } |
||
277 | $queue->$name = $data[$name]; |
||
278 | |||
279 | break; |
||
280 | case "timeout_to_redirect_to_extension": |
||
281 | case "number_unanswered_calls_to_redirect": |
||
282 | if (! array_key_exists($name, $data)) { |
||
283 | continue 2; |
||
284 | } |
||
285 | if (empty($data[$name])) { |
||
286 | $queue->$name = null; |
||
287 | } else { |
||
288 | $queue->$name = $data[$name]; |
||
289 | } |
||
290 | break; |
||
291 | case "timeout_extension": |
||
292 | if ( |
||
293 | ! array_key_exists($name, $data) |
||
294 | || empty($data[$name]) |
||
295 | || (array_key_exists('timeout_to_redirect_to_extension', $data) |
||
296 | && intval($data['timeout_to_redirect_to_extension']) === 0) |
||
297 | ) { |
||
298 | $queue->$name = null; |
||
299 | continue 2; |
||
300 | } |
||
301 | $queue->$name = $data[$name]; |
||
302 | |||
303 | break; |
||
304 | case "redirect_to_extension_if_unanswered": |
||
305 | if ( |
||
306 | ! array_key_exists($name, $data) |
||
307 | || empty($data[$name]) |
||
308 | || (array_key_exists('number_unanswered_calls_to_redirect', $data) |
||
309 | && intval($data['number_unanswered_calls_to_redirect']) === 0) |
||
310 | ) { |
||
311 | $queue->$name = null; |
||
312 | continue 2; |
||
313 | } |
||
314 | $queue->$name = $data[$name]; |
||
315 | |||
316 | break; |
||
317 | default: |
||
318 | if (! array_key_exists($name, $data)) { |
||
319 | continue 2; |
||
320 | } |
||
321 | $queue->$name = $data[$name]; |
||
322 | } |
||
323 | } |
||
324 | |||
325 | if ($queue->save() === false) { |
||
326 | $errors = $queue->getMessages(); |
||
327 | $this->flash->error(implode('<br>', $errors)); |
||
328 | |||
329 | return false; |
||
330 | } |
||
331 | |||
332 | return true; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Update the queue members with the provided data. |
||
337 | * |
||
338 | * @param array $data The data containing the queue members information. |
||
339 | * @return bool True if the update is successful, false otherwise. |
||
340 | */ |
||
341 | private function updateQueueMembers(array $data): bool |
||
400 | } |
||
401 | } |
||
402 |