| Conditions | 18 |
| Paths | 9217 |
| Total Lines | 135 |
| Code Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 237 | private static function restoreDefaultSettings(): PBXApiResult |
||
| 238 | { |
||
| 239 | $res = new PBXApiResult(); |
||
| 240 | $res->processor = __METHOD__; |
||
| 241 | $di = DI::getDefault(); |
||
| 242 | if ($di===null){ |
||
| 243 | $res->messages[]='Error on DI initialize'; |
||
| 244 | return $res; |
||
| 245 | } |
||
| 246 | |||
| 247 | $res->success = true; |
||
| 248 | $res->data['needRestartWorkers'] = true; |
||
| 249 | $rm = Util::which('rm'); |
||
| 250 | |||
| 251 | // Delete all providers |
||
| 252 | $records = Providers::find(); |
||
| 253 | if ( ! $records->delete()) { |
||
| 254 | $res->messages[] = $records->getMessages(); |
||
| 255 | $res->success = false; |
||
| 256 | } |
||
| 257 | |||
| 258 | // Delete routes |
||
| 259 | $records = OutgoingRoutingTable::find(); |
||
| 260 | if ( ! $records->delete()) { |
||
| 261 | $res->messages[] = $records->getMessages(); |
||
| 262 | $res->success = false; |
||
| 263 | } |
||
| 264 | |||
| 265 | $records = IncomingRoutingTable::find(); |
||
| 266 | if ( ! $records->delete()) { |
||
| 267 | $res->messages[] = $records->getMessages(); |
||
| 268 | $res->success = false; |
||
| 269 | } |
||
| 270 | |||
| 271 | // Delete out of work settings |
||
| 272 | $records = OutWorkTimes::find(); |
||
| 273 | if ( ! $records->delete()) { |
||
| 274 | $res->messages[] = $records->getMessages(); |
||
| 275 | $res->success = false; |
||
| 276 | } |
||
| 277 | |||
| 278 | // AMI Users |
||
| 279 | $records = AsteriskManagerUsers::find(); |
||
| 280 | if ( ! $records->delete()) { |
||
| 281 | $res->messages[] = $records->getMessages(); |
||
| 282 | $res->success = false; |
||
| 283 | } |
||
| 284 | |||
| 285 | // Pre delete some extensions type |
||
| 286 | // IVR Menu |
||
| 287 | $records = Extensions::find('type="'.Extensions::TYPE_IVR_MENU.'"'); |
||
| 288 | $records->delete(); |
||
| 289 | |||
| 290 | // CONFERENCE |
||
| 291 | $records = Extensions::find('type="'.Extensions::TYPE_CONFERENCE.'"'); |
||
| 292 | $records->delete(); |
||
| 293 | |||
| 294 | // QUEUE |
||
| 295 | $records = Extensions::find('type="'.Extensions::TYPE_QUEUE.'"'); |
||
| 296 | $records->delete(); |
||
| 297 | |||
| 298 | |||
| 299 | // Other extensions |
||
| 300 | $parameters = [ |
||
| 301 | 'conditions' => 'not number IN ({ids:array})', |
||
| 302 | 'bind' => [ |
||
| 303 | 'ids' => [ |
||
| 304 | '000063', //Reads back the extension |
||
| 305 | '000064', //0000MILLI |
||
| 306 | '10003246'//Echo test |
||
| 307 | ], |
||
| 308 | ], |
||
| 309 | ]; |
||
| 310 | $stopDeleting = false; |
||
| 311 | $countRecords = Extensions::count($parameters); |
||
| 312 | $deleteAttempts = 0; |
||
| 313 | while ($stopDeleting === false) { |
||
| 314 | $record = Extensions::findFirst($parameters); |
||
| 315 | if ($record === null) { |
||
| 316 | $stopDeleting = true; |
||
| 317 | continue; |
||
| 318 | } |
||
| 319 | if ( ! $record->delete()) { |
||
| 320 | $deleteAttempts += 1; |
||
| 321 | } |
||
| 322 | if ($deleteAttempts > $countRecords * 10) { |
||
| 323 | $stopDeleting = true; // Prevent loop |
||
| 324 | $res->messages[] = $record->getMessages(); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | // SoundFiles |
||
| 329 | $parameters = [ |
||
| 330 | 'conditions' => 'category = :custom:', |
||
| 331 | 'bind' => [ |
||
| 332 | 'custom' => SoundFiles::CATEGORY_CUSTOM, |
||
| 333 | ], |
||
| 334 | ]; |
||
| 335 | $records = SoundFiles::find($parameters); |
||
| 336 | |||
| 337 | foreach ($records as $record) { |
||
| 338 | if (stripos($record->path, '/storage/usbdisk1/mikopbx') !== false) { |
||
| 339 | Util::mwExec("{$rm} -rf {$record->path}"); |
||
| 340 | if ( ! $record->delete()) { |
||
| 341 | $res->messages[] = $record->getMessages(); |
||
| 342 | $res->success = false; |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | // PbxExtensions |
||
| 348 | $records = PbxExtensionModules::find(); |
||
| 349 | foreach ($records as $record) { |
||
| 350 | $moduleDir = PbxExtensionUtils::getModuleDir($record->uniqid); |
||
| 351 | Util::mwExec("{$rm} -rf {$moduleDir}"); |
||
| 352 | if ( ! $record->delete()) { |
||
| 353 | $res->messages[] = $record->getMessages(); |
||
| 354 | $res->success = false; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | // Delete CallRecords |
||
| 359 | $records = CallDetailRecords::find(); |
||
| 360 | if ( ! $records->delete()) { |
||
| 361 | $res->messages[] = $records->getMessages(); |
||
| 362 | $res->success = false; |
||
| 363 | } |
||
| 364 | |||
| 365 | // Delete CallRecords sound files |
||
| 366 | $callRecordsPath = $di->getShared('config')->path('asterisk.monitordir'); |
||
| 367 | if (stripos($callRecordsPath, '/storage/usbdisk1/mikopbx') !== false) { |
||
| 368 | Util::mwExec("{$rm} -rf {$callRecordsPath}/*"); |
||
| 369 | } |
||
| 370 | |||
| 371 | return $res; |
||
| 372 | } |
||
| 374 | } |