|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BadgeManager\Includes; |
| 4 | + |
| 5 | +use Exception; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class BadgeComposer |
| 9 | + * |
| 10 | + * This class is responsible for generating a coverage badge based on the input files provided. |
| 11 | + * |
| 12 | + * @package BadgeManager |
| 13 | + */ |
| 14 | +class BadgeComposer |
| 15 | +{ |
| 16 | + private array $inputFiles; |
| 17 | + private string $outputFile; |
| 18 | + private string $coverageName; |
| 19 | + public int $totalCoverage = 0; |
| 20 | + private int $totalElements = 0; |
| 21 | + private int $checkedElements = 0; |
| 22 | + |
| 23 | + /** |
| 24 | + * @throws Exception |
| 25 | + */ |
| 26 | + public function __construct(string $inputFiles, string $outputFile, string $coverageName = 'coverage') |
| 27 | + { |
| 28 | + $this->inputFiles = explode(',', $inputFiles); |
| 29 | + $this->outputFile = $outputFile; |
| 30 | + $this->coverageName = $coverageName; |
| 31 | + |
| 32 | + $this->validateFiles($this->inputFiles, $this->outputFile); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Validates the input files and output file. |
| 37 | + * |
| 38 | + * This method checks if the input files exist and if the output file is provided. |
| 39 | + * |
| 40 | + * @param array $inputFiles The array of input files to validate. |
| 41 | + * @param string $outputFile The output file to validate. |
| 42 | + * |
| 43 | + * @return void |
| 44 | + * @throws Exception If any of the input files do not exist or if the output file is not provided. |
| 45 | + */ |
| 46 | + private function validateFiles(array $inputFiles, string $outputFile): void |
| 47 | + { |
| 48 | + foreach ($inputFiles as $inputFile) { |
| 49 | + if (!file_exists($inputFile)) { |
| 50 | + throw new \Exception("input file does not exist: " . $inputFile); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (empty($outputFile)) { |
| 55 | + throw new \Exception("output file name is mandatory"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Runs the coverage process for each input file. |
| 61 | + * |
| 62 | + * This method iterates over the input files array and processes each file using the `processFile` method. |
| 63 | + * After processing all input files, the coverage is finalized using the `finalizeCoverage` method. |
| 64 | + * |
| 65 | + * @return void |
| 66 | + * @throws Exception |
| 67 | + */ |
| 68 | + public function run(): void |
| 69 | + { |
| 70 | + foreach ($this->inputFiles as $inputFile) { |
| 71 | + $this->processFile($inputFile); |
| 72 | + } |
| 73 | + $this->finalizeCoverage(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Process a file and update the total and checked elements count. |
| 78 | + * |
| 79 | + * @param string $inputFile The path to the XML file to process. |
| 80 | + * |
| 81 | + * @return void |
| 82 | + * @throws Exception When there is an error processing the file. |
| 83 | + * |
| 84 | + */ |
| 85 | + private function processFile(string $inputFile): void |
| 86 | + { |
| 87 | + try { |
| 88 | + $xml = new \SimpleXMLElement(file_get_contents($inputFile)); |
| 89 | + $metrics = $xml->xpath('//metrics'); |
| 90 | + foreach ($metrics as $metric) { |
| 91 | + $this->totalElements += (int) $metric['elements']; |
| 92 | + $this->checkedElements += (int) $metric['coveredelements']; |
| 93 | + } |
| 94 | + |
| 95 | + $coverage = round(($this->totalElements === 0) ? 0 : ($this->checkedElements / $this->totalElements) * 100); |
| 96 | + $this->totalCoverage += $coverage; |
| 97 | + } catch (Exception $e) { |
| 98 | + throw new Exception('Error processing file: ' . $inputFile); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Finalize the coverage report by generating a badge with the average coverage across all input files. |
| 104 | + * |
| 105 | + * @return void |
| 106 | + * @throws Exception If there is an error generating the badge. |
| 107 | + */ |
| 108 | + private function finalizeCoverage(): void |
| 109 | + { |
| 110 | + $totalCoverage = $this->totalCoverage / count($this->inputFiles); // Average coverage across all files |
| 111 | + $template = file_get_contents(__DIR__ . '/../badge-templates/badge.svg'); |
| 112 | + |
| 113 | + $template = str_replace('{{ total }}', $totalCoverage, $template); |
| 114 | + |
| 115 | + $template = str_replace('{{ coverage }}', $this->coverageName, $template); |
| 116 | + |
| 117 | + $color = '#a4a61d'; // Yellow-Green |
| 118 | + if ($totalCoverage < 40) { |
| 119 | + $color = '#e05d44'; // Red |
| 120 | + } elseif ($totalCoverage < 60) { |
| 121 | + $color = '#fe7d37'; // Orange |
| 122 | + } elseif ($totalCoverage < 75) { |
| 123 | + $color = '#dfb317'; // Yellow |
| 124 | + } elseif ($totalCoverage < 95) { |
| 125 | + $color = '#97CA00'; // Green |
| 126 | + } elseif ($totalCoverage <= 100) { |
| 127 | + $color = '#4c1'; // Bright Green |
| 128 | + } |
| 129 | + |
| 130 | + $template = str_replace('{{ total }}', $totalCoverage, $template); |
| 131 | + $template = str_replace('{{ color }}', $color, $template); |
| 132 | + |
| 133 | + file_put_contents($this->outputFile, $template); |
| 134 | + } |
| 135 | +} |
0 commit comments