accessibility_scanner-8.x-1.0-alpha8/src/Controller/AxeCoreCliHistoryController.php
src/Controller/AxeCoreCliHistoryController.php
<?php
namespace Drupal\accessibility_scanner\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\State\StateInterface;
use Drupal\web_page_archive\Entity\WebPageArchiveInterface;
use Drupal\web_page_archive\Entity\WebPageArchiveRunInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Displays the historical report.
*/
class AxeCoreCliHistoryController extends ControllerBase {
/**
* Constructs a new AxeCoreCliHistoryController.
*
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(StateInterface $state, DateFormatterInterface $date_formatter) {
$this->state = $state;
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('state'),
$container->get('date.formatter')
);
}
/**
* Generates the aggregate report data.
*
* @param \Drupal\web_page_archive\Entity\WebPageArchiveRunInterface $run
* The web page archive run entity.
*
* @return array
* Report array.
*/
public static function generateAggregateReportData(WebPageArchiveRunInterface $run) {
$report_data = [
'#all_urls' => [],
'#tags' => [],
];
foreach ($run->getCapturedArray() as $capture) {
$unserialized = unserialize($capture->getString());
if (get_class($unserialized['capture_response']) !== '__PHP_Incomplete_Class') {
$response = $unserialized['capture_response'];
$url = $response->getCaptureUrl();
$report = $response->getAggregateReportData();
$report_data[$url] = $report;
foreach ($report as $tag => $details) {
if (!isset($report_data['#tags'][$tag])) {
$report_data['#tags'][$tag] = TRUE;
$report_data['#passing'][$tag] = 0;
$report_data['#failing'][$tag] = 0;
}
foreach ($details as $type => $severities) {
foreach ($severities as $severity => $counts) {
if (!isset($report_data['#all_urls'][$tag][$type][$severity])) {
$report_data['#all_urls'][$tag][$type][$severity]['distinct'] = 0;
$report_data['#all_urls'][$tag][$type][$severity]['total'] = 0;
}
$report_data['#all_urls'][$tag][$type][$severity]['distinct'] += $counts['distinct'];
$report_data['#all_urls'][$tag][$type][$severity]['total'] += $counts['total'];
}
}
$pass_fail_key = !empty($report_data[$url][$tag]['violations']['#all_severities']['total']) ? '#failing' : '#passing';
$report_data[$pass_fail_key][$tag]++;
}
}
}
ksort($report_data);
return $report_data;
}
/**
* Displays the body for the historical report.
*
* @param \Drupal\web_page_archive\Entity\WebPageArchiveInterface $web_page_archive
* The web page archive config entity.
*
* @return array
* Render array for the historical report.
*/
public function content(WebPageArchiveInterface $web_page_archive) {
$queue_name = AxeCoreCliAggregateBatchController::getQueueName($web_page_archive);
// If this is our first time viewing the report, go to the process form.
$state_array = $this->state->get($queue_name);
if (!isset($state_array)) {
return $this->redirect('entity.web_page_archive.axecore_cli_history.process', ['web_page_archive' => $web_page_archive->id()]);
}
// Ensure sorted order.
ksort($state_array);
$ret = [
'#theme' => 'wpa-axecore-cli-history',
'#attached' => [
'drupalSettings' => [
'axeCoreCliReportData' => $state_array,
],
],
];
return $ret;
}
/**
* Displays the title for the historical report.
*
* @param \Drupal\web_page_archive\Entity\WebPageArchiveInterface $web_page_archive
* The web page archive config entity.
*
* @return string
* Title of the historical report.
*/
public function title(WebPageArchiveInterface $web_page_archive) {
return $this->t('@job Historical Report', ['@job' => $web_page_archive->label()]);
}
}
