Total Complexity | 59 |
Total Lines | 373 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like OutOffWorkTimeController 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 OutOffWorkTimeController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class OutOffWorkTimeController extends BaseController |
||
33 | { |
||
34 | /** |
||
35 | * This function retrieves OutWorkTimes data and formats it into an array that is used to display on the index page. |
||
36 | */ |
||
37 | public function indexAction(): void |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Changes the priority of the OutWorkTimes data based on the provided priority table. |
||
90 | * @return void |
||
91 | */ |
||
92 | public function changePriorityAction(): void |
||
93 | { |
||
94 | $this->view->disable(); |
||
95 | $result = true; |
||
96 | |||
97 | if (! $this->request->isPost()) { |
||
98 | return; |
||
99 | } |
||
100 | $priorityTable = $this->request->getPost(); |
||
101 | $rules = OutWorkTimes::find(); |
||
102 | foreach ($rules as $rule) { |
||
103 | if (array_key_exists($rule->id, $priorityTable)) { |
||
104 | $rule->priority = $priorityTable[$rule->id]; |
||
105 | $result .= $rule->update(); |
||
106 | } |
||
107 | } |
||
108 | echo json_encode($result); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * This function modifies the OutWorkTimes data based on the provided ID. |
||
113 | * |
||
114 | * @param string $id - The ID of the OutWorkTimes data to modify. (Optional) |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | public function modifyAction(string $id = ''): void |
||
119 | { |
||
120 | // Find the OutWorkTimes data based on the provided ID |
||
121 | // or create a new one if the ID is not provided or not found. |
||
122 | $timeFrame = OutWorkTimes::findFirstById($id); |
||
123 | if ($timeFrame === null) { |
||
124 | $timeFrame = new OutWorkTimes(); |
||
125 | } |
||
126 | |||
127 | // Create an array to store the extensions to forward calls to and populate it with the default value. |
||
128 | $forwardingExtensions = []; |
||
129 | $forwardingExtensions[""] = $this->translation->_("ex_SelectNumber"); |
||
130 | |||
131 | // Set the parameters to find extensions matching the extension in the OutWorkTimes data. |
||
132 | $parameters = [ |
||
133 | 'conditions' => 'number = :extension:', |
||
134 | 'bind' => [ |
||
135 | 'extension' => $timeFrame->extension, |
||
136 | ], |
||
137 | ]; |
||
138 | |||
139 | // Find the extensions matching the specified parameters and add them to the $forwardingExtensions array. |
||
140 | $extensions = Extensions::find($parameters); |
||
141 | foreach ($extensions as $record) { |
||
142 | $forwardingExtensions[$record->number] = $record->getRepresent(); |
||
143 | } |
||
144 | |||
145 | // Create an array to store the available audio messages and populate it with the default value. |
||
146 | $audioMessages = []; |
||
147 | $audioMessages[""] = $this->translation->_("sf_SelectAudioFile"); |
||
148 | |||
149 | // Find the sound files with the "custom" category and add them to the $audioMessages array. |
||
150 | $soundFiles = SoundFiles::find('category="custom"'); |
||
151 | foreach ($soundFiles as $record) { |
||
152 | $audioMessages[$record->id] = $record->name; |
||
153 | } |
||
154 | |||
155 | // Create an array to store the available actions and populate it with the default values. |
||
156 | $availableActions = [ |
||
157 | 'playmessage' => $this->translation->_('tf_SelectActionPlayMessage'), |
||
158 | 'extension' => $this->translation->_('tf_SelectActionRedirectToExtension'), |
||
159 | ]; |
||
160 | |||
161 | // Create an array to store the available week days and populate it with the default values. |
||
162 | $weekDays = ['-1' => '-']; |
||
163 | for ($i = "1"; $i <= 7; $i++) { |
||
164 | $weekDays[$i] = $this->translation->_(date('D', strtotime("Sunday +$i days"))); |
||
165 | } |
||
166 | |||
167 | // Create a new TimeFrameEditForm object with the $timeFrame |
||
168 | // and arrays for the forwarding extensions, audio messages, available actions, and week days. |
||
169 | $form = new TimeFrameEditForm( |
||
170 | $timeFrame, |
||
171 | [ |
||
172 | 'extensions' => $forwardingExtensions, |
||
173 | 'audio-message' => $audioMessages, |
||
174 | 'available-actions' => $availableActions, |
||
175 | 'week-days' => $weekDays, |
||
176 | ] |
||
177 | ); |
||
178 | |||
179 | // Set the form and the represented value of the $timeFrame object to the view. |
||
180 | $this->view->form = $form; |
||
181 | $this->view->represent = $timeFrame->getRepresent(); |
||
182 | |||
183 | // Get the list of allowed routing rules for the specified time condition ID. |
||
184 | $parameters = [ |
||
185 | 'columns' => 'routId AS rule_id', |
||
186 | 'conditions' => 'timeConditionId=:timeConditionId:', |
||
187 | 'bind' => [ |
||
188 | 'timeConditionId' => $id, |
||
189 | ], |
||
190 | ]; |
||
191 | $allowedRules = OutWorkTimesRouts::find($parameters)->toArray(); |
||
192 | $allowedRulesIds = array_column($allowedRules, 'rule_id'); |
||
193 | |||
194 | |||
195 | $filter = [ |
||
196 | 'conditions' => 'type="friend"', |
||
197 | 'columns' => 'host,port,uniqid,registration_type', |
||
198 | ]; |
||
199 | $data = Sip::find($filter)->toArray(); |
||
200 | $providersId = []; |
||
201 | foreach ($data as $providerData) { |
||
202 | if ($providerData['registration_type'] === Sip::REG_TYPE_INBOUND || empty($providerData['host'])) { |
||
203 | $providersId[$providerData['uniqid']] = $providerData['uniqid']; |
||
204 | } else { |
||
205 | $providersId[$providerData['uniqid']] |
||
206 | = SIPConf::getContextId($providerData['host'], $providerData['port']); |
||
207 | } |
||
208 | } |
||
209 | unset($data); |
||
210 | |||
211 | // Get the list of allowed routing rules |
||
212 | $rules = IncomingRoutingTable::find(['order' => 'provider,number,priority', 'conditions' => 'id>1']); |
||
213 | $routingTable = []; |
||
214 | foreach ($rules as $rule) { |
||
215 | $provider = $rule->Providers; |
||
216 | if ($provider) { |
||
217 | $modelType = ucfirst($provider->type); |
||
218 | $provByType = $provider->$modelType; |
||
219 | } else { |
||
220 | $provByType = new SIP(); |
||
221 | } |
||
222 | $extension = $rule->Extensions; |
||
223 | $values = [ |
||
224 | 'id' => $rule->id, |
||
225 | 'rulename' => $rule->rulename, |
||
226 | 'priority' => $rule->priority, |
||
227 | 'number' => $rule->number, |
||
228 | 'timeout' => $rule->timeout, |
||
229 | 'provider' => $rule->Providers ? $rule->Providers->getRepresent() : '', |
||
230 | 'provider-uniqid' => $rule->Providers ? $rule->Providers->uniqid : 'none', |
||
231 | 'context-id' => $rule->Providers ? $providersId[$rule->Providers->uniqid] : 'none', |
||
232 | 'disabled' => $provByType->disabled, |
||
233 | 'extension' => $rule->extension, |
||
234 | 'callerid' => $extension ? $extension->getRepresent() : '', |
||
235 | 'note' => $rule->note, |
||
236 | 'status' => in_array($rule->id, $allowedRulesIds, false) ? '' : 'disabled', |
||
237 | ]; |
||
238 | |||
239 | $routingTable[] = $values; |
||
240 | } |
||
241 | |||
242 | $this->view->rules = $routingTable; |
||
243 | |||
244 | // Prepare time zone offset |
||
245 | $dateTime = new \DateTime(); |
||
246 | $this->view->setVar('serverOffset', $dateTime->getOffset() / 60); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Saves out of work time for a user |
||
251 | */ |
||
252 | public function saveAction(): void |
||
253 | { |
||
254 | // Check if the request method is POST |
||
255 | if (! $this->request->isPost()) { |
||
256 | return; |
||
257 | } |
||
258 | |||
259 | // Get the data from the POST request |
||
260 | $data = $this->request->getPost(); |
||
261 | |||
262 | // Begin a database transaction |
||
263 | $this->db->begin(); |
||
264 | |||
265 | // Find the out-of-work time record by ID, or create a new record if it doesn't exist |
||
266 | $timeFrame = OutWorkTimes::findFirstByid($data['id']); |
||
267 | if ($timeFrame === null) { |
||
268 | $timeFrame = new OutWorkTimes(); |
||
269 | } |
||
270 | |||
271 | // Set the user parameters based on the data in the POST request |
||
272 | foreach ($timeFrame as $name => $value) { |
||
273 | switch ($name) { |
||
274 | case 'weekday_from': |
||
275 | case 'weekday_to': |
||
276 | if (! array_key_exists($name, $data)) { |
||
277 | $timeFrame->$name = ''; |
||
278 | } else { |
||
279 | $timeFrame->$name = ($data[$name] < 1) ? null : $data[$name]; |
||
280 | } |
||
281 | break; |
||
282 | case 'allowRestriction': |
||
283 | if (isset($data[$name])) { |
||
284 | $timeFrame->$name = ($data[$name] === 'on') ? '1' : '0'; |
||
285 | } else { |
||
286 | $timeFrame->$name = '0'; |
||
287 | } |
||
288 | break; |
||
289 | case 'calType': |
||
290 | $timeFrame->$name = ($data[$name] === 'none') ? '' : $data[$name]; |
||
291 | break; |
||
292 | case 'date_from': |
||
293 | case 'date_to': |
||
294 | case 'time_from': |
||
295 | case 'time_to': |
||
296 | if (! array_key_exists($name, $data)) { |
||
297 | $timeFrame->$name = ''; |
||
298 | } else { |
||
299 | $timeFrame->$name = $data[$name]; |
||
300 | } |
||
301 | break; |
||
302 | default: |
||
303 | if (! array_key_exists($name, $data)) { |
||
304 | continue 2; |
||
305 | } |
||
306 | $timeFrame->$name = $data[$name]; |
||
307 | } |
||
308 | } |
||
309 | |||
310 | // If the action is 'playmessage', set the extension to an empty string |
||
311 | if ('playmessage' === $timeFrame->action) { |
||
312 | $timeFrame->extension = ''; |
||
313 | } |
||
314 | |||
315 | // Save the out-of-work time record to the database |
||
316 | $error = !$timeFrame->save(); |
||
317 | |||
318 | // If there was an error saving the record, display the error message |
||
319 | if ($error === false) { |
||
320 | $errors = $timeFrame->getMessages(); |
||
321 | $this->flash->warning(implode('<br>', $errors)); |
||
322 | } |
||
323 | |||
324 | // If the ID is empty, set the reload parameter to the modified record's ID |
||
325 | if (empty($data['id'])) { |
||
326 | $this->view->reload = "out-off-work-time/modify/$timeFrame->id"; |
||
327 | } |
||
328 | |||
329 | // If there was no error saving the record, save the allowed outbound rules |
||
330 | if (!$error) { |
||
331 | $data['id'] = $timeFrame->id; |
||
332 | $error = ! $this->saveAllowedOutboundRules($data); |
||
333 | } |
||
334 | |||
335 | // If there was an error, rollback the database transaction and set the success parameter to false |
||
336 | if ($error) { |
||
337 | $this->view->success = false; |
||
338 | $this->db->rollback(); |
||
339 | } else { // Otherwise, commit the transaction, display a success message, and set the success parameter to true |
||
340 | $this->flash->success($this->translation->_('ms_SuccessfulSaved')); |
||
341 | $this->view->success = true; |
||
342 | $this->db->commit(); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Saves allowed outbound rules for a given time condition |
||
348 | * |
||
349 | * @param array $data |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | private function saveAllowedOutboundRules(array $data): bool |
||
354 | { |
||
355 | // Step 1: Delete old links to rules associated with the time condition. |
||
356 | $parameters = [ |
||
357 | 'conditions' => 'timeConditionId=:timeConditionId:', |
||
358 | 'bind' => [ |
||
359 | 'timeConditionId' => $data['id'], |
||
360 | ], |
||
361 | ]; |
||
362 | $oldRules = OutWorkTimesRouts::find($parameters); |
||
363 | if ($oldRules->delete() === false) { |
||
364 | $errors = $oldRules->getMessages(); |
||
365 | $this->flash->error(implode('<br>', $errors)); |
||
366 | |||
367 | return false; |
||
368 | } |
||
369 | |||
370 | // 2. Writes the allowed outbound rules to the database for the time condition. |
||
371 | foreach ($data as $key => $value) { |
||
372 | if (substr_count($key, 'rule-') > 0) { |
||
373 | $rule_id = explode('rule-', $key)[1]; |
||
374 | if ($value === 'on') { |
||
375 | $newRule = new OutWorkTimesRouts(); |
||
376 | $newRule->timeConditionId = $data['id']; |
||
377 | $newRule->routId = $rule_id; |
||
378 | if ($newRule->save() === false) { |
||
379 | $errors = $newRule->getMessages(); |
||
380 | $this->flash->error(implode('<br>', $errors)); |
||
381 | return false; |
||
382 | } |
||
383 | } |
||
384 | } |
||
385 | } |
||
386 | |||
387 | return true; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Deletes the OutWorkTimes record with the specified id. |
||
392 | * |
||
393 | * @param string $id The id of the OutWorkTimes record to delete. |
||
394 | * @return void |
||
395 | */ |
||
396 | public function deleteAction(string $id = ''): void |
||
405 | } |
||
406 | } |
||
407 |