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