|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @author andrey-tech |
| 5 | + * @copyright 2025 andrey-tech |
| 6 | + * @link https://github.com/andrey-tech/ |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace AndreyTech\PHPUnit\Cobertura\Formatter; |
| 13 | + |
| 14 | +use AndreyTech\PHPUnit\Cobertura\Formatter\Config\CommandLine; |
| 15 | +use AndreyTech\PHPUnit\Cobertura\Formatter\Config\File as ConfigFile; |
| 16 | +use AndreyTech\PHPUnit\Cobertura\Formatter\Parser\File as CoberturaFile; |
| 17 | +use AndreyTech\PHPUnit\Cobertura\Formatter\Renderer\Colorizer; |
| 18 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 19 | +use Throwable; |
| 20 | + |
| 21 | +use function sprintf; |
| 22 | + |
| 23 | +final class Application |
| 24 | +{ |
| 25 | + private const int EXIT_CODE_OK = 0; |
| 26 | + private const int EXIT_CODE_ERROR = 1; |
| 27 | + |
| 28 | + private ConsoleOutput $consoleOutput; |
| 29 | + private Stats $stats; |
| 30 | + |
| 31 | + public function __construct() |
| 32 | + { |
| 33 | + $this->consoleOutput = new ConsoleOutput(); |
| 34 | + $this->stats = new Stats(); |
| 35 | + } |
| 36 | + |
| 37 | + public function run(): int |
| 38 | + { |
| 39 | + $this->stats->start(); |
| 40 | + |
| 41 | + try { |
| 42 | + $exitCode = $this->doRun(); |
| 43 | + } catch (Throwable $exception) { |
| 44 | + $exitCode = self::EXIT_CODE_ERROR; |
| 45 | + $this->error( |
| 46 | + sprintf('ERROR: %s', $exception->getMessage()) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + $this->stats->finish(); |
| 51 | + $this->showStats($exitCode); |
| 52 | + |
| 53 | + return $exitCode; |
| 54 | + } |
| 55 | + |
| 56 | + private function doRun(): int |
| 57 | + { |
| 58 | + (new Renderer( |
| 59 | + $this->consoleOutput, |
| 60 | + new Colorizer( |
| 61 | + new ConfigFile() |
| 62 | + ) |
| 63 | + ))->render( |
| 64 | + (new Parser())->parse( |
| 65 | + new CoberturaFile( |
| 66 | + (new CommandLine())->coberturaFile() |
| 67 | + ) |
| 68 | + ) |
| 69 | + ); |
| 70 | + |
| 71 | + return self::EXIT_CODE_OK; |
| 72 | + } |
| 73 | + |
| 74 | + private function message(string $message): void |
| 75 | + { |
| 76 | + $this->consoleOutput->writeln($message); |
| 77 | + } |
| 78 | + |
| 79 | + private function error(string $message): void |
| 80 | + { |
| 81 | + $this->consoleOutput->writeln( |
| 82 | + sprintf('<fg=red;options=bold>%s</>', $message) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private function showStats(int $exitCode): void |
| 87 | + { |
| 88 | + $this->message( |
| 89 | + sprintf( |
| 90 | + 'Exit code: %s, Time: %s, Memory: %s.', |
| 91 | + $exitCode, |
| 92 | + $this->stats->time(), |
| 93 | + $this->stats->memory() |
| 94 | + ) |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments