You are here

class EntityPagerAdvice in Entity Pager 7

Class EntityPagerAdvice.

Provides a central and compact section of code where all performance advice logic and messages are handled.

Hierarchy

Expanded class hierarchy of EntityPagerAdvice

File

includes/EntityPagerAdvice.inc, line 14
A class to manage advice when using the Entity Pager module.

View source
class EntityPagerAdvice extends EntityPagerSetup {

  /**
   * Check performance.
   *
   * See if the option in the View is set to set to log performance.
   * see: View > Format > Settings > Log performance suggestions.
   *
   * @return bool
   *   True mean check performance, false mean do not check performance.
   */
  private function checkPerformance() {
    $check_performance = FALSE;

    // Get Views settings.
    $settings = $this
      ->getSettings($this
      ->getView());
    if ($settings['next_prev']['log_performance']) {
      $check_performance = TRUE;
    }
    return $check_performance;
  }

  /**
   * Get Cache status of View.
   *
   * A system state function on whether Views caching is configured on the View.
   *
   * @return bool
   *   True or false on whether views chaching is used.
   */
  private function viewsCachingUsed() {
    $view = $this
      ->getView();
    $cache_value = $view->query->pager->display->handler->default_display->display->display_options['cache']['type'];
    $cache_result = TRUE;
    if ($cache_value == 'none') {
      $cache_result = FALSE;
    }
    return $cache_result;
  }

  /**
   * Run advice logic.
   *
   * Run the logic that manages performance advice.
   *
   * @param string $advice_type
   *   The type of advice.
   * @param string $issue
   *   The specific issue.
   */
  protected function runAdviceLogic($advice_type, $issue) {
    if ($this
      ->checkPerformance()) {

      // Log performance suggestions.
      if ($advice_type == 'quantity') {

        // Coding note:  ideally nested 'if' statements should be avoided,
        // but the whole purpose of this little logic method is to sort
        // the logic in one manageable place. The if statements are simple
        // (single purpose) meaning they are easy to work down logically.
        if ($this
          ->getViewResultCount() > 500) {
          if ($this
            ->viewsCachingUsed()) {

            // Do nothing.
          }
          else {
            $this
              ->logAdvice($advice_type, $issue);
          }
        }
      }
      else {
        $this
          ->logAdvice($advice_type, $issue);
      }
    }
  }

  /**
   * Log advice.
   *
   * Create a Performance Message and log it watchdog.
   *
   * @param string $advice_type
   *   The type of advice.
   * @param string $issue
   *   The specific issue.
   */
  private function logAdvice($advice_type, $issue) {
    $view = $this
      ->getView();
    switch ($advice_type) {
      case 'no-records':
        watchdog('entity_pager', "Performance Advice: in the View ('@view_name'),\n                the issue ('@reason'), means you are needlessly attempting to\n                displaying an empty Entity Pager block on this page.\n                To ignore this advice, you can turn this alert off at:\n                @view_name view > Format > Settings > Log performance\n                suggestions.", array(
          '@view_name' => $view->human_name,
          '@reason' => $issue,
        ), WATCHDOG_INFO);
        break;
      case 'quantity':
        watchdog('entity_pager', "Performance Advice: in the View ('@view_name'),\n                the issue ('@reason'). At the moment there are 500+ records in\n                your pager. The Entity Pager module is constructed to be fast\n                keeping processing to a minimum, that said, it might still be a\n                good idea to cache the Views Query Result see:\n                @view_name view > Other > Caching. (Note: do not cache the\n                Rendered output). To ignore this advice, you can turn this\n                alert off at: @view_name view > Format > Settings >\n                Log performance suggestions.", array(
          '@view_name' => $view->human_name,
          '@reason' => $issue,
        ), WATCHDOG_INFO);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityPager::$default protected property
EntityPager::getDefault public function Get Default value.
EntityPager::setDefaults public function Set the default values for Entity Pager.
EntityPagerAdvice::checkPerformance private function Check performance.
EntityPagerAdvice::logAdvice private function Log advice.
EntityPagerAdvice::runAdviceLogic protected function Run advice logic.
EntityPagerAdvice::viewsCachingUsed private function Get Cache status of View.
EntityPagerSetup::$entity protected property
EntityPagerSetup::$entityInfo protected property
EntityPagerSetup::$outSettings protected property
EntityPagerSetup::$view protected property
EntityPagerSetup::establishEntity protected function Establish Entity.
EntityPagerSetup::establishEntityInfo protected function Establish the Entity Info from the View.
EntityPagerSetup::getAllTitle public function Get All Title.
EntityPagerSetup::getAllUrl public function Get All URL.
EntityPagerSetup::getCountWord public function Get result count word.
EntityPagerSetup::getEntity public function Get Entity.
EntityPagerSetup::getEntityInfo public function Get Entity Information.
EntityPagerSetup::getEntityKeys protected function Get Entity Keys.
EntityPagerSetup::getField protected function Generic 'Getter' for fields.
EntityPagerSetup::getFields public function Get Fields.
EntityPagerSetup::getIdFieldName protected function Get ID Field Name.
EntityPagerSetup::getSettings public function Get Views Settings.
EntityPagerSetup::getView public function Get View.
EntityPagerSetup::getViewResult public function Get View Result.
EntityPagerSetup::getViewResultCount public function Get View result count.
EntityPagerSetup::setAllTitle public function Set All Title.
EntityPagerSetup::setAllUrl public function Set Link All Url.
EntityPagerSetup::setEntity public function Set Entity.
EntityPagerSetup::setEntityInfo protected function Set Entity Information.
EntityPagerSetup::setField protected function Generic 'Setter' for fields.
EntityPagerSetup::setIdFieldName protected function Set Field Name of ID field.
EntityPagerSetup::setView public function Set View.
EntityPagerSetup::__construct public function Setup Entity Pager with minimum values to work with. Overrides EntityPager::__construct