SiteAuditReportBase.php in Site Audit 8.3
File
src/Plugin/SiteAuditReportBase.php
View source
<?php
namespace Drupal\site_audit\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
abstract class SiteAuditReportBase extends PluginBase implements SiteAuditReportInterface {
use StringTranslationTrait;
protected $checks;
protected $percent;
protected $scoreMax = 0;
protected $scoreTotal = 0;
protected $hasFail = FALSE;
protected $registry;
protected function getReportName() {
return $this
->getPluginDefinition()['name'];
}
public function getLabel() {
return $this
->getPluginDefinition()['name'];
}
public function getDescription() {
return $this
->getPluginDefinition()['description'];
}
public function getPercent() {
return $this->percent ?: SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO;
}
public function __construct($configuration, $plugin_id, $plugin_definition) {
$checkManager = \Drupal::service('plugin.manager.site_audit_check');
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->registry = new \stdClass();
$percent_override = NULL;
$checks_to_skip = [];
$checks_to_perform = $this
->getChecksList();
foreach ($checks_to_perform as $key => $check_name) {
if (in_array($key, $checks_to_skip)) {
unset($checks_to_perform[$key]);
}
}
if (empty($checks_to_perform)) {
$this_def = $this
->getPluginDefinition();
}
$config = \Drupal::config('site_audit');
foreach ($checks_to_perform as $check_id) {
$opt_out = $config
->get('opt_out.' . $this
->getPluginId() . $check_id) != NULL;
$check = $checkManager
->createInstance($check_id, [
'registry' => $this->registry,
'opt_out' => $opt_out,
'options' => $this->configuration,
]);
if ($check
->getScore() != SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO) {
if ($check
->getScore() == SiteAuditCheckBase::AUDIT_CHECK_SCORE_FAIL) {
$this->hasFail = TRUE;
}
$this->scoreTotal += $check
->getScore();
$this->scoreMax += SiteAuditCheckBase::AUDIT_CHECK_SCORE_PASS;
}
if ($check
->getPercentOverride()) {
$percent_override = $check
->getPercentOverride();
}
$this->checks[$check_id] = $check;
if ($check
->shouldAbort()) {
break;
}
}
if ($percent_override) {
$this->percent = $percent_override;
}
else {
if ($this->scoreMax != 0) {
$this->percent = round($this->scoreTotal / $this->scoreMax * 100);
}
}
}
public function getChecksList() {
$this_def = $this
->getPluginDefinition();
$checkManager = \Drupal::service('plugin.manager.site_audit_check');
static $checkDefinitions = NULL;
if (empty($checkDefinitions)) {
$checkDefinitions = $checkManager
->getDefinitions();
}
$checksInReport = [];
foreach ($checkDefinitions as $checkDefinition) {
if ($checkDefinition['report'] == $this_def['id']) {
$checksInReport[$checkDefinition['id']] = $checkDefinition;
}
}
uasort($checksInReport, [
$this,
'weightKeySort',
]);
$checks = [];
foreach ($checksInReport as $check) {
$checks[] = $check['id'];
}
return $checks;
}
public function weightKeySort($a, $b) {
if ($a['weight'] > $b['weight']) {
return 1;
}
elseif ($a['weight'] < $b['weight']) {
return -1;
}
return strcmp($a['id'], $b['id']);
}
public function getCheckObjects() {
return $this->checks;
}
}