Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander committed Jun 23, 2023
1 parent 324b702 commit 002f9d5
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Console/CliCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace IWD\Symfony\PresentationBundle\Console;

use IWD\Symfony\PresentationBundle\Exception\ValidatorException;
use IWD\Symfony\PresentationBundle\Interfaces\InputContractInterface;
use IWD\Symfony\PresentationBundle\Service\CliContractResolver;
use ReflectionClass;
Expand All @@ -12,6 +13,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

abstract class CliCommand extends Command
{
Expand Down Expand Up @@ -89,8 +91,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

/** @var InputContractInterface $inputContract */
$inputContract = $this->cliContractResolver->resolve($input, static::getInputContractClass());
try {
/** @var InputContractInterface $inputContract */
$inputContract = $this->cliContractResolver->resolve($input, static::getInputContractClass());
} catch (ValidatorException $exception) {
$violations = json_decode($exception->getMessage(), true, 512, JSON_THROW_ON_ERROR);
$message = 'Command options has violations:' . PHP_EOL;

$i = 0;
foreach ($violations as $property => $violation) {
++$i;
$message .= sprintf(
'%s. %s: %s',
$i, $property, $violation
) . PHP_EOL;
}
$io->error($message);

return self::FAILURE;
}

return $this->handle(
io: $io,
Expand Down

0 comments on commit 002f9d5

Please sign in to comment.