Skip to content

Commit

Permalink
Merge pull request #30 from 21TORR/next
Browse files Browse the repository at this point in the history
Add `hosting:validate-app` command
  • Loading branch information
apfelbox authored Oct 23, 2024
2 parents 65f9c7f + cd7d122 commit e62d2c8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* (feature) Add health checks (live + ready).
* (improvement) Bump Janus and update CI.
* (feature) Add default doctrine check integration.
* (feature) Add `hosting:validate-app` command for usage in the CI.


3.1.0
Expand Down
56 changes: 56 additions & 0 deletions src/Command/ValidateAppCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

namespace Torr\Hosting\Command;

use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Torr\Cli\Console\Style\TorrStyle;
use Torr\Hosting\Event\ValidateAppEvent;

/**
* @final
*/
#[AsCommand(
"hosting:validate-app",
description: "Validates the app configuration, for usage in the CI before deployment",
)]
class ValidateAppCommand extends Command
{
/**
*/
public function __construct (
private readonly EventDispatcherInterface $dispatcher,
)
{
parent::__construct();
}

/**
*/
protected function execute (InputInterface $input, OutputInterface $output) : int
{
$io = new TorrStyle($input, $output);
$io->title("Hosting: Validate App");

$event = new ValidateAppEvent($io);
$this->dispatcher->dispatch($event);
$failedCheck = $event->getFailedCheck();

if (null !== $failedCheck)
{
$io->error(\sprintf(
"Validation failed: %s",
$failedCheck,
));

return self::FAILURE;
}

$io->success("App is valid");

return self::SUCCESS;
}
}
52 changes: 52 additions & 0 deletions src/Event/ValidateAppEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);

namespace Torr\Hosting\Event;

use Symfony\Contracts\EventDispatcher\Event;
use Torr\Cli\Console\Style\TorrStyle;

/**
* This event is dispatched to validate the app.
* It is supposed to check internal configuration and settings.
*
* Be aware, that it is supposed to be run in the CI, so you might not have access to some external services (like
* the database).
*
* @final
*/
class ValidateAppEvent extends Event
{
private ?string $failedCheck = null;

/**
*
*/
public function __construct (
public readonly TorrStyle $io,
) {}

/**
*
*/
public function markAppAsInvalid (string $failedCheck) : void
{
$this->failedCheck = $failedCheck;
$this->stopPropagation();
}

/**
*
*/
public function isValid () : bool
{
return null === $this->failedCheck;
}

/**
*
*/
public function getFailedCheck () : ?string
{
return $this->failedCheck;
}
}

0 comments on commit e62d2c8

Please sign in to comment.