Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
32 / 32 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| OlzCommand | |
100.00% |
32 / 32 |
|
100.00% |
4 / 4 |
13 | |
100.00% |
1 / 1 |
| getAllowedAppEnvs | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| handle | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| execute | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
8 | |||
| getAppEnv | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getIdent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| logAndOutput | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Command\Common; |
| 4 | |
| 5 | use Olz\Utils\WithUtilsTrait; |
| 6 | use Symfony\Component\Console\Command\Command; |
| 7 | use Symfony\Component\Console\Input\InputInterface; |
| 8 | use Symfony\Component\Console\Output\OutputInterface; |
| 9 | |
| 10 | abstract class OlzCommand extends Command { |
| 11 | use WithUtilsTrait; |
| 12 | |
| 13 | protected ?OutputInterface $output = null; |
| 14 | |
| 15 | /** @return array<string> */ |
| 16 | abstract protected function getAllowedAppEnvs(): array; |
| 17 | |
| 18 | abstract protected function handle(InputInterface $input, OutputInterface $output): int; |
| 19 | |
| 20 | protected function execute(InputInterface $input, OutputInterface $output): int { |
| 21 | $this->output = $output; |
| 22 | |
| 23 | try { |
| 24 | $app_env = $this->getAppEnv(); |
| 25 | $allowed_app_envs = $this->getAllowedAppEnvs(); |
| 26 | $allowed = false; |
| 27 | foreach ($allowed_app_envs as $allowed_app_env) { |
| 28 | if ($app_env === $allowed_app_env) { |
| 29 | $allowed = true; |
| 30 | } |
| 31 | } |
| 32 | if (!$allowed) { |
| 33 | $this->logAndOutput("Command {$this->getIdent()} not allowed in app env {$app_env}.", level: 'notice'); |
| 34 | return Command::INVALID; |
| 35 | } |
| 36 | $this->logAndOutput("Running command {$this->getIdent()}..."); |
| 37 | $status = $this->handle($input, $output); |
| 38 | if ($status === Command::SUCCESS) { |
| 39 | $this->logAndOutput("Successfully ran command {$this->getIdent()}."); |
| 40 | } elseif ($status === Command::FAILURE) { |
| 41 | $this->logAndOutput("Failed running command {$this->getIdent()}.", level: 'notice'); |
| 42 | } elseif ($status === Command::INVALID) { |
| 43 | $this->logAndOutput("Command {$this->getIdent()} called with invalid arguments.", level: 'notice'); |
| 44 | } else { |
| 45 | $this->logAndOutput("Command {$this->getIdent()} finished with unknown status {$status}.", level: 'warning'); |
| 46 | } |
| 47 | } catch (\Exception $exc) { |
| 48 | $this->logAndOutput("Error running command {$this->getIdent()}: {$exc->getMessage()}.", level: 'error'); |
| 49 | $status = Command::FAILURE; |
| 50 | } |
| 51 | return $status; |
| 52 | } |
| 53 | |
| 54 | protected function getAppEnv(): string { |
| 55 | $olz_app_env = $this->envUtils()->getAppEnv(); |
| 56 | $symfony_app_env = $_ENV['APP_ENV'] ?? null; |
| 57 | if ($olz_app_env !== $symfony_app_env) { |
| 58 | throw new \Exception("OLZ and symfony app env do not match ({$olz_app_env} vs. {$symfony_app_env})"); |
| 59 | } |
| 60 | return $olz_app_env; |
| 61 | } |
| 62 | |
| 63 | protected function getIdent(): string { |
| 64 | return get_called_class(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param array<mixed> $context |
| 69 | * @param 'alert'|'critical'|'debug'|'emergency'|'error'|'info'|'notice'|'warning' $level |
| 70 | */ |
| 71 | protected function logAndOutput(string $message, array $context = [], string $level = 'info'): void { |
| 72 | $this->log()->log($level, $message, $context); |
| 73 | if ($this->output !== null) { |
| 74 | $this->output->writeln($message); |
| 75 | } |
| 76 | } |
| 77 | } |