| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 272 |
| Code Lines | 122 |
| Lines | 103 |
| Ratio | 37.87 % |
| Changes | 5 | ||
| 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 |
||
| 383 | public function __formAction() |
||
| 384 | { |
||
| 385 | $fields = $_POST['fields']; |
||
| 386 | $this->_errors = array(); |
||
| 387 | $providers = Symphony::ExtensionManager()->getProvidersOf(iProvider::EVENT); |
||
| 388 | $providerClass = null; |
||
| 389 | |||
| 390 | View Code Duplication | if (trim($fields['name']) === '') { |
|
| 391 | $this->_errors['name'] = __('This is a required field'); |
||
| 392 | } |
||
| 393 | |||
| 394 | View Code Duplication | if (trim($fields['source']) === '') { |
|
| 395 | $this->_errors['source'] = __('This is a required field'); |
||
| 396 | } |
||
| 397 | |||
| 398 | $filters = isset($fields['filters']) ? $fields['filters'] : array(); |
||
| 399 | |||
| 400 | // See if a Provided Datasource is saved |
||
| 401 | View Code Duplication | if (!empty($providers)) { |
|
| 402 | foreach ($providers as $providerClass => $provider) { |
||
| 403 | if ($fields['source'] === call_user_func(array($providerClass, 'getSource'))) { |
||
| 404 | call_user_func_array(array($providerClass, 'validate'), array(&$fields, &$this->_errors)); |
||
| 405 | break; |
||
| 406 | } |
||
| 407 | |||
| 408 | unset($providerClass); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | $classname = Lang::createHandle($fields['name'], 255, '_', false, true, |
||
| 413 | array('@^[^a-z\d]+@i' => '', '/[^\w-\.]/i' => '')); |
||
| 414 | $rootelement = str_replace('_', '-', $classname); |
||
| 415 | $extends = 'SectionEvent'; |
||
| 416 | |||
| 417 | // Check to make sure the classname is not empty after handlisation. |
||
| 418 | View Code Duplication | if (empty($classname) && !isset($this->_errors['name'])) { |
|
| 419 | $this->_errors['name'] = __('Please ensure name contains at least one Latin-based character.', |
||
| 420 | array($classname)); |
||
| 421 | } |
||
| 422 | |||
| 423 | $file = EVENTS . '/event.' . $classname . '.php'; |
||
| 424 | $isDuplicate = false; |
||
| 425 | $queueForDeletion = null; |
||
| 426 | |||
| 427 | View Code Duplication | if ($this->_context['action'] === 'new' && is_file($file)) { |
|
| 428 | $isDuplicate = true; |
||
| 429 | } elseif ($this->_context['action'] === 'edit') { |
||
| 430 | $existing_handle = $this->_context['handle']; |
||
| 431 | |||
| 432 | if ($classname !== $existing_handle && is_file($file)) { |
||
| 433 | $isDuplicate = true; |
||
| 434 | } elseif ($classname !== $existing_handle) { |
||
| 435 | $queueForDeletion = EVENTS . '/event.' . $existing_handle . '.php'; |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | // Duplicate |
||
| 440 | View Code Duplication | if ($isDuplicate) { |
|
| 441 | $this->_errors['name'] = __('An Event with the name %s already exists', |
||
| 442 | array('<code>' . $classname . '</code>')); |
||
| 443 | } |
||
| 444 | |||
| 445 | if (empty($this->_errors)) { |
||
| 446 | $source = $fields['source']; |
||
| 447 | $params = array( |
||
| 448 | 'rootelement' => $rootelement, |
||
| 449 | ); |
||
| 450 | |||
| 451 | $about = array( |
||
| 452 | 'name' => $fields['name'], |
||
| 453 | 'version' => 'Symphony ' . Symphony::Configuration()->get('version', 'symphony'), |
||
| 454 | 'release date' => DateTimeObj::getGMT('c'), |
||
| 455 | 'author name' => Symphony::Author()->getFullName(), |
||
| 456 | 'author website' => URL, |
||
| 457 | 'author email' => Symphony::Author()->get('email') |
||
| 458 | ); |
||
| 459 | |||
| 460 | // If there is a provider, get their template |
||
| 461 | if ($providerClass) { |
||
| 462 | $eventShell = file_get_contents(call_user_func(array($providerClass, 'getTemplate'))); |
||
| 463 | } else { |
||
| 464 | $eventShell = file_get_contents($this->getTemplate('blueprints.event')); |
||
| 465 | $about['trigger condition'] = $rootelement; |
||
| 466 | } |
||
| 467 | |||
| 468 | $this->__injectAboutInformation($eventShell, $about); |
||
| 469 | |||
| 470 | // Replace the name |
||
| 471 | $eventShell = str_replace('<!-- CLASS NAME -->', $classname, $eventShell); |
||
| 472 | |||
| 473 | // Build the templates |
||
| 474 | if ($providerClass) { |
||
| 475 | $eventShell = call_user_func(array($providerClass, 'prepare'), $fields, $params, $eventShell); |
||
| 476 | } else { |
||
| 477 | $this->__injectFilters($eventShell, $filters); |
||
| 478 | |||
| 479 | // Add Documentation |
||
| 480 | $ajaxEventDoc = new contentAjaxEventDocumentation(); |
||
| 481 | $doc_parts = array(); |
||
| 482 | |||
| 483 | // Add Documentation (Success/Failure) |
||
| 484 | $ajaxEventDoc->addEntrySuccessDoc($doc_parts, $rootelement, $filters); |
||
| 485 | $ajaxEventDoc->addEntryFailureDoc($doc_parts, $rootelement, $filters); |
||
| 486 | |||
| 487 | // Filters |
||
| 488 | $ajaxEventDoc->addDefaultFiltersDoc($doc_parts, $rootelement, $filters); |
||
| 489 | |||
| 490 | // Frontend Markup |
||
| 491 | $ajaxEventDoc->addFrontendMarkupDoc($doc_parts, $rootelement, $fields['source'], $filters); |
||
| 492 | $ajaxEventDoc->addSendMailFilterDoc($doc_parts, $filters); |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Allows adding documentation for new filters. A reference to the $documentation |
||
| 496 | * array is provided, along with selected filters |
||
| 497 | * |
||
| 498 | * @delegate AppendEventFilterDocumentation |
||
| 499 | * @param string $context |
||
| 500 | * '/blueprints/events/(edit|new|info)/' |
||
| 501 | * @param array $selected |
||
| 502 | * An array of all the selected filters for this Event |
||
| 503 | * @param array $documentation |
||
| 504 | * An array of all the documentation XMLElements, passed by reference |
||
| 505 | * @param string $rootelment |
||
| 506 | * The name of this event, as a handle. |
||
| 507 | */ |
||
| 508 | Symphony::ExtensionManager()->notifyMembers('AppendEventFilterDocumentation', '/blueprints/events/', |
||
| 509 | array( |
||
| 510 | 'selected' => $filters, |
||
| 511 | 'documentation' => &$doc_parts, |
||
| 512 | 'rootelement' => $rootelement |
||
| 513 | )); |
||
| 514 | |||
| 515 | $documentation = join(PHP_EOL, array_map(function ($part) { |
||
| 516 | return rtrim($part->generate(true, 4)); |
||
| 517 | }, $doc_parts)); |
||
| 518 | $documentation = str_replace('\'', '\\\'', $documentation); |
||
| 519 | |||
| 520 | $eventShell = str_replace('<!-- CLASS EXTENDS -->', $extends, $eventShell); |
||
| 521 | $eventShell = str_replace('<!-- DOCUMENTATION -->', General::tabsToSpaces($documentation, 4), |
||
| 522 | $eventShell); |
||
| 523 | } |
||
| 524 | |||
| 525 | $eventShell = str_replace('<!-- ROOT ELEMENT -->', $rootelement, $eventShell); |
||
| 526 | $eventShell = str_replace('<!-- CLASS NAME -->', $classname, $eventShell); |
||
| 527 | $eventShell = str_replace('<!-- SOURCE -->', $source, $eventShell); |
||
| 528 | |||
| 529 | // Remove left over placeholders |
||
| 530 | $eventShell = preg_replace(array('/<!--[\w ]++-->/'), '', $eventShell); |
||
| 531 | |||
| 532 | if ($this->_context['action'] === 'new') { |
||
| 533 | /** |
||
| 534 | * Prior to creating an Event, the file path where it will be written to |
||
| 535 | * is provided and well as the contents of that file. |
||
| 536 | * |
||
| 537 | * @delegate EventsPreCreate |
||
| 538 | * @since Symphony 2.2 |
||
| 539 | * @param string $context |
||
| 540 | * '/blueprints/events/' |
||
| 541 | * @param string $file |
||
| 542 | * The path to the Event file |
||
| 543 | * @param string $contents |
||
| 544 | * The contents for this Event as a string passed by reference |
||
| 545 | * @param array $filters |
||
| 546 | * An array of the filters attached to this event |
||
| 547 | */ |
||
| 548 | Symphony::ExtensionManager()->notifyMembers('EventPreCreate', '/blueprints/events/', array( |
||
| 549 | 'file' => $file, |
||
| 550 | 'contents' => &$eventShell, |
||
| 551 | 'filters' => $filters |
||
| 552 | )); |
||
| 553 | } else { |
||
| 554 | /** |
||
| 555 | * Prior to editing an Event, the file path where it will be written to |
||
| 556 | * is provided and well as the contents of that file. |
||
| 557 | * |
||
| 558 | * @delegate EventPreEdit |
||
| 559 | * @since Symphony 2.2 |
||
| 560 | * @param string $context |
||
| 561 | * '/blueprints/events/' |
||
| 562 | * @param string $file |
||
| 563 | * The path to the Event file |
||
| 564 | * @param string $contents |
||
| 565 | * The contents for this Event as a string passed by reference |
||
| 566 | * @param array $filters |
||
| 567 | * An array of the filters attached to this event |
||
| 568 | */ |
||
| 569 | Symphony::ExtensionManager()->notifyMembers('EventPreEdit', '/blueprints/events/', array( |
||
| 570 | 'file' => $file, |
||
| 571 | 'contents' => &$eventShell, |
||
| 572 | 'filters' => $filters |
||
| 573 | )); |
||
| 574 | } |
||
| 575 | |||
| 576 | // Write the file |
||
| 577 | View Code Duplication | if (!is_writable(dirname($file)) || !General::writeFile($file, $eventShell, |
|
| 578 | Symphony::Configuration()->get('write_mode', 'file')) |
||
| 579 | ) { |
||
| 580 | $this->pageAlert( |
||
| 581 | __('Failed to write Event to disk.') |
||
| 582 | . ' ' . __('Please check permissions on %s.', array('<code>/workspace/events</code>')), |
||
| 583 | Alert::ERROR |
||
| 584 | ); |
||
| 585 | |||
| 586 | // Write successful |
||
| 587 | } else { |
||
| 588 | if (function_exists('opcache_invalidate')) { |
||
| 589 | opcache_invalidate($file, true); |
||
| 590 | } |
||
| 591 | |||
| 592 | // Attach this event to pages |
||
| 593 | $connections = $fields['connections']; |
||
| 594 | ResourceManager::setPages(ResourceManager::RESOURCE_TYPE_EVENT, |
||
| 595 | is_null($existing_handle) ? $classname : $existing_handle, $connections); |
||
| 596 | |||
| 597 | if ($queueForDeletion) { |
||
| 598 | General::deleteFile($queueForDeletion); |
||
| 599 | |||
| 600 | $pages = PageManager::fetch(false, array('events', 'id'), array( |
||
| 601 | " |
||
| 602 | `events` REGEXP '[[:<:]]" . $existing_handle . "[[:>:]]' |
||
| 603 | " |
||
| 604 | )); |
||
| 605 | |||
| 606 | if (is_array($pages) && !empty($pages)) { |
||
| 607 | foreach ($pages as $page) { |
||
| 608 | $page['events'] = preg_replace('/\b' . $existing_handle . '\b/i', $classname, |
||
| 609 | $page['events']); |
||
| 610 | |||
| 611 | PageManager::edit($page['id'], $page); |
||
| 612 | } |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | if ($this->_context['action'] === 'new') { |
||
| 617 | /** |
||
| 618 | * After creating the Event, the path to the Event file is provided |
||
| 619 | * |
||
| 620 | * @delegate EventPostCreate |
||
| 621 | * @since Symphony 2.2 |
||
| 622 | * @param string $context |
||
| 623 | * '/blueprints/events/' |
||
| 624 | * @param string $file |
||
| 625 | * The path to the Event file |
||
| 626 | */ |
||
| 627 | Symphony::ExtensionManager()->notifyMembers('EventPostCreate', '/blueprints/events/', array( |
||
| 628 | 'file' => $file |
||
| 629 | )); |
||
| 630 | } else { |
||
| 631 | /** |
||
| 632 | * After editing the Event, the path to the Event file is provided |
||
| 633 | * |
||
| 634 | * @delegate EventPostEdit |
||
| 635 | * @since Symphony 2.2 |
||
| 636 | * @param string $context |
||
| 637 | * '/blueprints/events/' |
||
| 638 | * @param string $file |
||
| 639 | * The path to the Event file |
||
| 640 | * @param string $previous_file |
||
| 641 | * The path of the previous Event file in the case where an Event may |
||
| 642 | * have been renamed. To get the handle from this value, see |
||
| 643 | * `EventManager::__getHandleFromFilename` |
||
| 644 | */ |
||
| 645 | Symphony::ExtensionManager()->notifyMembers('EventPostEdit', '/blueprints/events/', array( |
||
| 646 | 'file' => $file, |
||
| 647 | 'previous_file' => ($queueForDeletion) ? $queueForDeletion : null |
||
| 648 | )); |
||
| 649 | } |
||
| 650 | |||
| 651 | redirect(SYMPHONY_URL . '/blueprints/events/edit/' . $classname . '/' . ($this->_context['action'] === 'new' ? 'created' : 'saved') . '/'); |
||
| 652 | } |
||
| 653 | } |
||
| 654 | } |
||
| 655 | |||
| 720 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: