Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review the validate command #671

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 73 additions & 45 deletions src/Console/Command/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

namespace KevinGH\Box\Console\Command;

use Exception;
use function count;
use KevinGH\Box\Configuration\Configuration;
use KevinGH\Box\Console\ConfigurationLoader;
use KevinGH\Box\Console\ConfigurationLocator;
use KevinGH\Box\Console\IO\IO;
Expand All @@ -24,6 +25,8 @@
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Throwable;
use Webmozart\Assert\Assert;

/**
* @private
Expand Down Expand Up @@ -75,70 +78,95 @@ protected function executeCommand(IO $io): int
$io,
false,
);
} catch (Throwable $unexpectedFailure) {
// Continue
}

if (isset($config)) {
return self::checkConfig($config, $io);
}

Assert::true(isset($unexpectedFailure));

return self::handleFailure($unexpectedFailure, $io);
}

private static function checkConfig(Configuration $config, IO $io): int
{
$ignoreRecommendationsAndWarnings = (bool) $io->getInput()->getOption(self::IGNORE_MESSAGES_OPTION);

$recommendations = $config->getRecommendations();
$warnings = $config->getWarnings();
$recommendations = $config->getRecommendations();
$warnings = $config->getWarnings();

MessageRenderer::render($io, $recommendations, $warnings);
MessageRenderer::render($io, $recommendations, $warnings);

$hasRecommendationsOrWarnings = [] === $recommendations && [] === $warnings;
$hasRecommendationsOrWarnings = 0 === count($recommendations) && 0 === count($warnings);

if (false === $hasRecommendationsOrWarnings) {
if ([] === $recommendations) {
$io->caution('The configuration file passed the validation with warnings.');
} elseif ([] === $warnings) {
$io->caution('The configuration file passed the validation with recommendations.');
} else {
$io->caution('The configuration file passed the validation with recommendations and warnings.');
}
if (false === $hasRecommendationsOrWarnings) {
if (0 === count($recommendations)) {
$io->caution('The configuration file passed the validation with warnings.');
} elseif (0 === count($warnings)) {
$io->caution('The configuration file passed the validation with recommendations.');
} else {
$io->success('The configuration file passed the validation.');
$io->caution('The configuration file passed the validation with recommendations and warnings.');
}

return $hasRecommendationsOrWarnings || $input->getOption(self::IGNORE_MESSAGES_OPTION) ? 0 : 1;
} catch (Exception $exception) {
// Continue
} else {
$io->success('The configuration file passed the validation.');
}

return $hasRecommendationsOrWarnings || $ignoreRecommendationsAndWarnings
? self::SUCCESS
: self::FAILURE;
}

private static function handleFailure(Throwable $throwable, IO $io): int
{
if ($io->isVerbose()) {
throw new RuntimeException(
sprintf(
'The configuration file failed validation: %s',
$exception->getMessage(),
$throwable->getMessage(),
),
$exception->getCode(),
$exception,
$throwable->getCode(),
$throwable,
);
}

if ($exception instanceof JsonValidationException) {
$io->writeln(
sprintf(
'<error>The configuration file failed validation: "%s" does not match the expected JSON '
.'schema:</error>',
$exception->getValidatedFile(),
),
);
return $throwable instanceof JsonValidationException
? self::handleJsonValidationFailure($throwable, $io)
: self::handleGenericFailure($throwable, $io);
}

$io->writeln('');
private static function handleJsonValidationFailure(JsonValidationException $exception, IO $io): int
{
$io->writeln(
sprintf(
'<error>The configuration file failed validation: "%s" does not match the expected JSON '
.'schema:</error>',
$exception->getValidatedFile(),
),
);

foreach ($exception->getErrors() as $error) {
$io->writeln("<comment> - $error</comment>");
}
} else {
$errorMessage = isset($exception)
? sprintf('The configuration file failed validation: %s', $exception->getMessage())
: 'The configuration file failed validation.'
;
$io->writeln('');

$io->writeln(
sprintf(
'<error>%s</error>',
$errorMessage,
),
);
foreach ($exception->getErrors() as $error) {
$io->writeln("<comment> - $error</comment>");
}

return 1;
return self::FAILURE;
}

private static function handleGenericFailure(Throwable $throwable, IO $io): int
{
$errorMessage = sprintf('The configuration file failed validation: %s', $throwable->getMessage());

$io->writeln(
sprintf(
'<error>%s</error>',
$errorMessage,
),
);

return self::FAILURE;
}
}