public function run(ContextInterface $context)
{
$configuration = $this->getConfiguration();
$percentage = round(min(100, max(0, (double) $configuration['level'])), 2);
$cloverFile = $configuration['clover_file'];
if (!$this->filesystem->exists($cloverFile)) {
return TaskResult::createFailed($this, $context, 'Invalid input file provided');
}
if (!$percentage) {
return TaskResult::createFailed($this, $context, 'An integer checked percentage must be given as second parameter');
}
$xml = new SimpleXMLElement($this->filesystem->readFromFileInfo(new SplFileInfo($cloverFile)));
$totalElements = (string) current($xml->xpath('/coverage/project/metrics/@elements'));
$checkedElements = (string) current($xml->xpath('/coverage/project/metrics/@coveredelements'));
$coverage = round($checkedElements / $totalElements * 100, 2);
if ($coverage < $percentage) {
$message = sprintf('Code coverage is %1$d%%, which is below the accepted %2$d%%' . PHP_EOL, $coverage, $percentage);
return TaskResult::createFailed($this, $context, $message);
}
return TaskResult::createPassed($this, $context);
}