| Total Complexity | 57 |
| Total Lines | 358 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SaveRecordAction 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 SaveRecordAction, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class SaveRecordAction extends Injectable |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * Saves a record with associated entities. |
||
| 47 | * |
||
| 48 | * @param array $data Data to be saved. |
||
| 49 | * @return PBXApiResult Result of the save operation. |
||
| 50 | */ |
||
| 51 | public static function main(array $data): PBXApiResult |
||
| 52 | { |
||
| 53 | // Initialize the result object |
||
| 54 | $res = new PBXApiResult(); |
||
| 55 | $res->processor = __METHOD__; |
||
| 56 | $res->success = true; |
||
| 57 | |||
| 58 | $di = Di::getDefault(); |
||
| 59 | $db = $di->get(MainDatabaseProvider::SERVICE_NAME); |
||
| 60 | $db->begin(); |
||
| 61 | |||
| 62 | $dataStructure = new DataStructure($data); |
||
| 63 | |||
| 64 | // Save user entity |
||
| 65 | list($userEntity, $res->success) = self::saveUser($dataStructure); |
||
| 66 | if (!$res->success) { |
||
| 67 | // Handle errors and rollback |
||
| 68 | $res->messages['error'][] = $userEntity->getMessages(); |
||
| 69 | $db->rollback(); |
||
| 70 | return $res; |
||
| 71 | } else { |
||
| 72 | $dataStructure->user_id = $userEntity->id; |
||
| 73 | } |
||
| 74 | |||
| 75 | // Save extension entity |
||
| 76 | list($extension, $res->success) = self::saveExtension($dataStructure, false); |
||
| 77 | if (!$res->success) { |
||
| 78 | // Handle errors and rollback |
||
| 79 | $res->messages['error'][] = implode($extension->getMessages()); |
||
| 80 | $db->rollback(); |
||
| 81 | return $res; |
||
| 82 | } |
||
| 83 | |||
| 84 | // Save SIP entity |
||
| 85 | list($sipEntity, $res->success) = self::saveSip($dataStructure); |
||
| 86 | if (!$res->success) { |
||
| 87 | // Handle errors and rollback |
||
| 88 | $res->messages['error'][] = implode($sipEntity->getMessages()); |
||
| 89 | $db->rollback(); |
||
| 90 | return $res; |
||
| 91 | } |
||
| 92 | |||
| 93 | // Save forwarding rights entity |
||
| 94 | list($fwdEntity, $res->success) = self::saveForwardingRights($dataStructure); |
||
| 95 | if (!$res->success) { |
||
| 96 | // Handle errors and rollback |
||
| 97 | $res->messages['error'][] = implode($fwdEntity->getMessages()); |
||
| 98 | $db->rollback(); |
||
| 99 | return $res; |
||
| 100 | } |
||
| 101 | |||
| 102 | // Check mobile number presence and save related entities |
||
| 103 | if (!empty($dataStructure->mobile_number)) { |
||
| 104 | |||
| 105 | // Save mobile extension |
||
| 106 | list($mobileExtension, $res->success) = self::saveExtension($dataStructure, true); |
||
| 107 | if (!$res->success) { |
||
| 108 | // Handle errors and rollback |
||
| 109 | $res->messages['error'][] = implode($mobileExtension->getMessages()); |
||
| 110 | $db->rollback(); |
||
| 111 | return $res; |
||
| 112 | } |
||
| 113 | |||
| 114 | // Save ExternalPhones for mobile number |
||
| 115 | list($externalPhone, $res->success) = self::saveExternalPhones($dataStructure); |
||
| 116 | if (!$res->success) { |
||
| 117 | // Handle errors and rollback |
||
| 118 | $res->messages['error'][] = implode($externalPhone->getMessages()); |
||
| 119 | $db->rollback(); |
||
| 120 | return $res; |
||
| 121 | } |
||
| 122 | } else { |
||
| 123 | // Delete mobile number if it was associated with the user |
||
| 124 | list($deletedMobileNumber, $res->success) = self::deleteMobileNumber($userEntity); |
||
| 125 | if (!$res->success) { |
||
| 126 | $res->messages['error'][] = implode($deletedMobileNumber->getMessages()); |
||
| 127 | $db->rollback(); |
||
| 128 | return $res; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | $db->commit(); |
||
| 132 | |||
| 133 | $res = GetRecordAction::main($extension->id); |
||
| 134 | $res->processor = __METHOD__; |
||
| 135 | return $res; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Save parameters to the Users table |
||
| 140 | * |
||
| 141 | * @param DataStructure $dataStructure The data structure containing the input data. |
||
| 142 | * @return array An array containing the saved Users entity and the save result. |
||
| 143 | */ |
||
| 144 | private static function saveUser(DataStructure $dataStructure): array |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Save the extension for a user. |
||
| 172 | * @param DataStructure $dataStructure The data structure containing the input data. |
||
| 173 | * @param bool $isMobile Flag indicating if it's a mobile extension. |
||
| 174 | * |
||
| 175 | * @return array An array containing the saved Extensions entity and the save result. |
||
| 176 | */ |
||
| 177 | private static function saveExtension(DataStructure $dataStructure, bool $isMobile = false): array |
||
| 178 | { |
||
| 179 | $parameters = []; |
||
| 180 | $parameters['conditions'] = 'type=:type: AND is_general_user_number = "1" AND userid=:userid:'; |
||
| 181 | $parameters['bind']['type'] = $isMobile ? Extensions::TYPE_EXTERNAL : Extensions::TYPE_SIP; |
||
| 182 | $parameters['bind']['userid'] = $dataStructure->user_id ; |
||
| 183 | |||
| 184 | $extension = Extensions::findFirst($parameters); |
||
| 185 | if ($extension === null) { |
||
| 186 | $extension = new Extensions(); |
||
| 187 | } |
||
| 188 | $metaData = Di::getDefault()->get(ModelsMetadataProvider::SERVICE_NAME); |
||
| 189 | foreach ($metaData->getAttributes($extension) as $name) { |
||
| 190 | switch ($name) { |
||
| 191 | case 'id': |
||
| 192 | // Skip saving the 'id' field |
||
| 193 | break; |
||
| 194 | case 'type': |
||
| 195 | // Set the 'type' based on the value of $isMobile |
||
| 196 | $extension->$name = $isMobile ? Extensions::TYPE_EXTERNAL : Extensions::TYPE_SIP; |
||
| 197 | break; |
||
| 198 | case 'callerid': |
||
| 199 | // Sanitize the caller ID based on 'user_username' |
||
| 200 | $extension->$name = self::sanitizeCallerId( $dataStructure->user_username); |
||
| 201 | break; |
||
| 202 | case 'userid': |
||
| 203 | // Set 'userid' to the ID of the user entity |
||
| 204 | $extension->$name = $dataStructure->user_id; |
||
| 205 | break; |
||
| 206 | case 'number': |
||
| 207 | // Set 'number' based on the value of mobile_number or number |
||
| 208 | $extension->$name = $isMobile ? $dataStructure->mobile_number : $dataStructure->number; |
||
| 209 | break; |
||
| 210 | case 'search-index': |
||
| 211 | // Generate search index for the extension |
||
| 212 | $extension->$name = self::generateSearchIndex($dataStructure, $isMobile); |
||
| 213 | break; |
||
| 214 | default: |
||
| 215 | if (property_exists($dataStructure, $name)) { |
||
| 216 | // Set other fields based on the values in $data |
||
| 217 | $extension->$name = $dataStructure->$name; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | $result = $extension->save(); |
||
| 222 | return [$extension, $result]; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Save the SIP entity with the provided data. |
||
| 227 | * |
||
| 228 | * @param DataStructure $dataStructure The data structure containing the input data. |
||
| 229 | * @return array An array containing the saved SIP entity and the save result. |
||
| 230 | */ |
||
| 231 | private static function saveSip(DataStructure $dataStructure): array |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Save the ExtensionForwardingRights entity with the provided data. |
||
| 278 | * |
||
| 279 | * @param DataStructure $dataStructure The data structure containing the input data. |
||
| 280 | * @return array An array containing the saved ExtensionForwardingRights entity and the save result. |
||
| 281 | */ |
||
| 282 | private static function saveForwardingRights(DataStructure $dataStructure): array |
||
| 283 | { |
||
| 284 | $forwardingRight = ExtensionForwardingRights::findFirstByExtension($dataStructure->number); |
||
| 285 | if ($forwardingRight === null) { |
||
| 286 | $forwardingRight = new ExtensionForwardingRights(); |
||
| 287 | } |
||
| 288 | $metaData = Di::getDefault()->get(ModelsMetadataProvider::SERVICE_NAME); |
||
| 289 | foreach ($metaData->getAttributes($forwardingRight) as $name) { |
||
| 290 | switch ($name) { |
||
| 291 | case 'extension': |
||
| 292 | // Set 'extension' based on the value of number |
||
| 293 | $forwardingRight->$name = $dataStructure->number; |
||
| 294 | break; |
||
| 295 | case 'ringlength': |
||
| 296 | $forwardingRight->ringlength = 0; |
||
| 297 | if (!empty($dataStructure->fwd_ringlength)) { |
||
| 298 | $forwardingRight->ringlength = $dataStructure->fwd_ringlength; |
||
| 299 | } elseif (!empty($dataStructure->fwd_forwarding)) { |
||
| 300 | $forwardingRight->ringlength = 45; |
||
| 301 | } |
||
| 302 | break; |
||
| 303 | default: |
||
| 304 | $propertyKey = 'fwd_' . $name; |
||
| 305 | if (property_exists($dataStructure, $propertyKey)) { |
||
| 306 | // Set other fields based on the other fields in $dataStructure |
||
| 307 | $forwardingRight->$name = $dataStructure->$propertyKey === -1 ? '' : $dataStructure->$propertyKey; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | $result = $forwardingRight->save(); |
||
| 312 | return [$forwardingRight, $result]; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Save parameters to the ExternalPhones table for a mobile number. |
||
| 317 | * |
||
| 318 | * @param DataStructure $dataStructure The data structure containing the input data. |
||
| 319 | * @return array An array containing the saved ExternalPhones entity and the save result. |
||
| 320 | */ |
||
| 321 | private static function saveExternalPhones(DataStructure $dataStructure): array |
||
| 322 | { |
||
| 323 | $externalPhone = ExternalPhones::findFirstByUniqid($dataStructure->mobile_uniqid); |
||
| 324 | if ($externalPhone === null) { |
||
| 325 | $externalPhone = new ExternalPhones(); |
||
| 326 | } |
||
| 327 | $metaData = Di::getDefault()->get(ModelsMetadataProvider::SERVICE_NAME); |
||
| 328 | foreach ($metaData->getAttributes($externalPhone) as $name) { |
||
| 329 | switch ($name) { |
||
| 330 | case 'extension': |
||
| 331 | $externalPhone->$name = $dataStructure->mobile_number; |
||
| 332 | break; |
||
| 333 | case 'description': |
||
| 334 | $externalPhone->$name = $dataStructure->user_username; |
||
| 335 | break; |
||
| 336 | default: |
||
| 337 | $propertyKey = 'mobile_' . $name; |
||
| 338 | if (property_exists($dataStructure, $propertyKey)) { |
||
| 339 | // Set other fields based on the other fields in $dataStructure |
||
| 340 | $externalPhone->$name = $dataStructure->$propertyKey; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | $result = $externalPhone->save(); |
||
| 346 | return [$externalPhone, $result]; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Delete a mobile number associated with a user. |
||
| 351 | * |
||
| 352 | * @param Users $userEntity The user entity. |
||
| 353 | * @return array An array containing the deleted mobile number entity and the deletion result. |
||
| 354 | */ |
||
| 355 | private static function deleteMobileNumber(Users $userEntity): array |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Generate a search index for the extension. |
||
| 374 | * |
||
| 375 | * @param DataStructure $dataStructure The data structure containing the input data. |
||
| 376 | * @param bool $isMobile Flag indicating if it's a mobile extension. |
||
| 377 | * @return string The generated search index. |
||
| 378 | */ |
||
| 379 | private static function generateSearchIndex(DataStructure $dataStructure, bool $isMobile = false): string |
||
| 380 | { |
||
| 381 | // Collect data for the search index |
||
| 382 | $username = mb_strtolower($dataStructure->user_username); |
||
| 383 | $callerId = mb_strtolower(self::sanitizeCallerId($dataStructure->user_username)); |
||
| 384 | $email = mb_strtolower($dataStructure->user_email); |
||
| 385 | $internalNumber = mb_strtolower($dataStructure->number); |
||
| 386 | $mobileNumber = $isMobile ? mb_strtolower($dataStructure->mobile_number) : ''; |
||
| 387 | |||
| 388 | // Combine all fields into a single string |
||
| 389 | return $username . ' ' . $callerId . ' ' . $email . ' ' . $internalNumber . ' ' . $mobileNumber; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Sanitize the caller ID by removing non-alphanumeric characters. |
||
| 394 | * |
||
| 395 | * @param string $callerId |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | private static function sanitizeCallerId(string $callerId): string |
||
| 401 | } |
||
| 402 | } |