Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| LogsCommandResults | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| notice | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| success | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| failure | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Fast Forward Development Tools for PHP projects. |
| 7 | * |
| 8 | * This file is part of fast-forward/dev-tools project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/ |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | */ |
| 19 | |
| 20 | namespace FastForward\DevTools\Console\Command\Traits; |
| 21 | |
| 22 | use Psr\Log\LogLevel; |
| 23 | use Symfony\Component\Console\Command\Command; |
| 24 | use Symfony\Component\Console\Input\InputInterface; |
| 25 | |
| 26 | /** |
| 27 | * Provides reusable helpers for logging command outcomes and returning exit codes. |
| 28 | * |
| 29 | * Consuming commands stay focused on orchestration while this trait keeps the |
| 30 | * success, notice, and failure logging shape consistent across the command |
| 31 | * surface. |
| 32 | */ |
| 33 | trait LogsCommandResults |
| 34 | { |
| 35 | use HasCommandLogger; |
| 36 | |
| 37 | /** |
| 38 | * Logs an informational command message at notice level. |
| 39 | * |
| 40 | * @param string $message the notice message |
| 41 | * @param InputInterface $input the originating command input |
| 42 | * @param array<string, mixed> $context optional extra log context |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | private function notice(string $message, InputInterface $input, array $context = []): void |
| 47 | { |
| 48 | $this->getLogger() |
| 49 | ->notice($message, [ |
| 50 | 'input' => $input, |
| 51 | ...$context, |
| 52 | ]); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Logs a successful command result and returns the success exit code. |
| 57 | * |
| 58 | * @param string $message the success message |
| 59 | * @param InputInterface $input the originating command input |
| 60 | * @param array<string, mixed> $context optional extra log context |
| 61 | * @param string $logLevel the PSR-3 log level used for the successful result |
| 62 | * |
| 63 | * @return int |
| 64 | */ |
| 65 | private function success( |
| 66 | string $message, |
| 67 | InputInterface $input, |
| 68 | array $context = [], |
| 69 | string $logLevel = LogLevel::INFO, |
| 70 | ): int { |
| 71 | $context = [ |
| 72 | 'input' => $input, |
| 73 | ...$context, |
| 74 | ]; |
| 75 | |
| 76 | $this->getLogger() |
| 77 | ->log($logLevel, $message, $context); |
| 78 | |
| 79 | return Command::SUCCESS; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Logs a failed command result and returns the failure exit code. |
| 84 | * |
| 85 | * @param string $message the failure message |
| 86 | * @param InputInterface $input the originating command input |
| 87 | * @param array<string, mixed> $context optional extra log context |
| 88 | * @param string|null $file the related file path when known |
| 89 | * @param int|null $line the related line when known |
| 90 | * |
| 91 | * @return int |
| 92 | */ |
| 93 | private function failure( |
| 94 | string $message, |
| 95 | InputInterface $input, |
| 96 | array $context = [], |
| 97 | ?string $file = null, |
| 98 | ?int $line = null, |
| 99 | ): int { |
| 100 | $this->getLogger() |
| 101 | ->error($message, [ |
| 102 | 'input' => $input, |
| 103 | 'file' => $file, |
| 104 | 'line' => $line, |
| 105 | ...$context, |
| 106 | ]); |
| 107 | |
| 108 | return Command::FAILURE; |
| 109 | } |
| 110 | } |