Redirect404SensorPlugin.php in Monitoring 8        
                          
                  
                        
  
  
  
  
  
File
  src/Plugin/monitoring/SensorPlugin/Redirect404SensorPlugin.php
  
    View source  
  <?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\monitoring\Result\SensorResultInterface;
use Drupal\monitoring\SensorPlugin\ExtendedInfoSensorPluginInterface;
class Redirect404SensorPlugin extends DatabaseAggregatorSensorPlugin implements ExtendedInfoSensorPluginInterface {
  
  protected $configurableConditions = FALSE;
  
  protected $configurableVerboseOutput = FALSE;
  
  protected $configurableTable = FALSE;
  
  protected function addAggregateExpression(SelectInterface $select) {
    $select
      ->addField('redirect_404', 'daily_count', 'records_count');
  }
  
  public function getAggregateQuery() {
    $query = parent::getAggregateQuery();
    $query
      ->addField('redirect_404', 'path');
    
    $query
      ->condition('resolved', 0);
    $query
      ->orderBy('daily_count', 'DESC');
    $query
      ->range(0, 1);
    return $query;
  }
  
  public function getQuery() {
    $query = parent::getQuery();
    
    $order =& $query
      ->getOrderBy();
    $order = [];
    $query
      ->orderBy('daily_count', 'DESC');
    $query
      ->condition('resolved', 0);
    $query
      ->range(0, 10);
    return $query;
  }
  
  protected function buildTableHeader($rows = []) {
    $header = parent::buildTableHeader($rows);
    if (isset($header['path'])) {
      $header['path'] = $this
        ->t('Path');
      $header['count'] = $this
        ->t('Count');
      $header['daily_count'] = $this
        ->t('Daily count');
      $header['timestamp'] = $this
        ->t('Last access');
    }
    return $header;
  }
  
  public function runSensor(SensorResultInterface $result) {
    parent::runSensor($result);
    if (!empty($this->fetchedObject) && !empty($this->fetchedObject->path)) {
      $result
        ->addStatusMessage($this->fetchedObject->path);
    }
  }
  
  public function getDefaultConfiguration() {
    $default_config = [
      'settings' => [
        'table' => 'redirect_404',
      ],
    ];
    return $default_config;
  }
}