From 9e5e7bfe7ffc24f95c9cb32dc7af01cee4a64ce8 Mon Sep 17 00:00:00 2001 From: sebprt Date: Wed, 25 Oct 2023 12:13:29 +0200 Subject: [PATCH] Improved exceptions management + request body --- .../Pipeline/AddBeforePipelineStepCommand.php | 1 - .../Workflow/RemoveWorkflowCommand.php | 2 +- src/Cloud/DeclareWorkflowFailedException.php | 7 +++ src/Cloud/Event/Workflow/WorkflowRemoved.php | 15 +++++++ .../DeclareWorkflowCommandHandler.php | 43 ++++++++++++++++--- .../Workflow/RemoveWorkflowCommandHandler.php | 20 +++++++-- src/Cloud/RemoveWorkflowFailedException.php | 7 +++ 7 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 src/Cloud/DeclareWorkflowFailedException.php create mode 100644 src/Cloud/Event/Workflow/WorkflowRemoved.php create mode 100644 src/Cloud/RemoveWorkflowFailedException.php diff --git a/src/Cloud/Command/Pipeline/AddBeforePipelineStepCommand.php b/src/Cloud/Command/Pipeline/AddBeforePipelineStepCommand.php index 3f95f747..f3b6e282 100644 --- a/src/Cloud/Command/Pipeline/AddBeforePipelineStepCommand.php +++ b/src/Cloud/Command/Pipeline/AddBeforePipelineStepCommand.php @@ -8,7 +8,6 @@ use Kiboko\Component\Satellite\Cloud\DTO\PipelineId; use Kiboko\Component\Satellite\Cloud\DTO\Step; use Kiboko\Component\Satellite\Cloud\DTO\StepCode; - final class AddBeforePipelineStepCommand implements Command { public function __construct( diff --git a/src/Cloud/Command/Workflow/RemoveWorkflowCommand.php b/src/Cloud/Command/Workflow/RemoveWorkflowCommand.php index c4e00bba..770b75c8 100644 --- a/src/Cloud/Command/Workflow/RemoveWorkflowCommand.php +++ b/src/Cloud/Command/Workflow/RemoveWorkflowCommand.php @@ -10,6 +10,6 @@ final class RemoveWorkflowCommand implements Command { public function __construct( - public WorkflowId $workflow, + public WorkflowId $id, ) {} } diff --git a/src/Cloud/DeclareWorkflowFailedException.php b/src/Cloud/DeclareWorkflowFailedException.php new file mode 100644 index 00000000..5c9c38af --- /dev/null +++ b/src/Cloud/DeclareWorkflowFailedException.php @@ -0,0 +1,7 @@ +id; + } +} diff --git a/src/Cloud/Handler/Workflow/DeclareWorkflowCommandHandler.php b/src/Cloud/Handler/Workflow/DeclareWorkflowCommandHandler.php index 85e131d4..130facd9 100644 --- a/src/Cloud/Handler/Workflow/DeclareWorkflowCommandHandler.php +++ b/src/Cloud/Handler/Workflow/DeclareWorkflowCommandHandler.php @@ -16,7 +16,6 @@ public function __construct( public function __invoke(Cloud\Command\Workflow\DeclareWorkflowCommand $command): Cloud\Event\Workflow\WorkflowDeclared { - $result = null; try { /** @var Api\Model\WorkflowDeclareWorkflowCommandJsonldRead $result */ $result = $this->client->declareWorkflowWorkflowCollection( @@ -42,14 +41,46 @@ public function __invoke(Cloud\Command\Workflow\DeclareWorkflowCommand $command) ->setType($repository->type) ->setUrl($repository->url) )) - ), + ) + ->setJobs( + $command->jobs->map( + function (Cloud\DTO\Workflow\JobInterface $job) { + if ($job instanceof Cloud\DTO\Workflow\Pipeline) { + return (new Api\Model\Job()) + ->setCode($job->code->asString()) + ->setLabel($job->label) + ->setPipeline( + (new Api\Model\Pipeline()) + ->setSteps( + $job->stepList->map( + fn (Cloud\DTO\Step $step) => (new Api\Model\Step()) + ->setCode($step->code->asString()) + ->setLabel($step->label) + ->setConfiguration($step->config) + ) + ) + ); + } + + if ($job instanceof Cloud\DTO\Workflow\Action) { + return (new Api\Model\Job()) + ->setCode($job->code->asString()) + ->setLabel($job->label) + ->setAction( + (new Api\Model\Action()) + ->setConfiguration($job->configuration) + ); + } + + throw new \RuntimeException('Unexpected instance of PipelineInterface.'); + } + ) + ), ); } catch (Api\Exception\DeclareWorkflowWorkflowCollectionBadRequestException $exception) { - throw new Cloud\DeclarePipelineFailedException('Something went wrong while declaring the pipeline. Maybe your client is not up to date, you may want to update your Gyroscops client.', previous: $exception); + throw new Cloud\DeclareWorkflowFailedException('Something went wrong while declaring the workflow. Maybe your client is not up to date, you may want to update your Gyroscops client.', previous: $exception); } catch (Api\Exception\DeclareWorkflowWorkflowCollectionUnprocessableEntityException $exception) { - throw new Cloud\DeclarePipelineFailedException('Something went wrong while declaring the pipeline. It seems the data you sent was invalid, please check your input.', previous: $exception); - } catch (\Exception $exception) { - var_dump($exception); + throw new Cloud\DeclareWorkflowFailedException('Something went wrong while declaring the workflow. It seems the data you sent was invalid, please check your input.', previous: $exception); } return new Cloud\Event\Workflow\WorkflowDeclared($result->getId()); diff --git a/src/Cloud/Handler/Workflow/RemoveWorkflowCommandHandler.php b/src/Cloud/Handler/Workflow/RemoveWorkflowCommandHandler.php index 81f24a60..5bd2b43f 100644 --- a/src/Cloud/Handler/Workflow/RemoveWorkflowCommandHandler.php +++ b/src/Cloud/Handler/Workflow/RemoveWorkflowCommandHandler.php @@ -4,17 +4,31 @@ namespace Kiboko\Component\Satellite\Cloud\Handler\Workflow; +use Gyroscops\Api; use Kiboko\Component\Satellite\Cloud; -final class RemoveWorkflowCommandHandler +final readonly class RemoveWorkflowCommandHandler { public function __construct( - \Gyroscops\Api\Client $client, + private \Gyroscops\Api\Client $client, ) { } - public function __invoke(Cloud\Command\Workflow\DeclareWorkflowCommand $command) + public function __invoke(Cloud\Command\Workflow\RemoveWorkflowCommand $command): Cloud\Event\Workflow\WorkflowRemoved { + try { + /** @var Api\Model\WorkflowRemoveWorkflowCommandJsonldRead $result */ + $result = $this->client->softDeleteWorkflowItem( + $command->id->asString() + ); + } catch (Api\Exception\SoftDeleteWorkflowItemBadRequestException $exception) { + throw new Cloud\RemoveWorkflowFailedException('Something went wrong while removing the workflow. Maybe your client is not up to date, you may want to update your Gyroscops client.', previous: $exception); + } catch (Api\Exception\SoftDeleteWorkflowItemUnprocessableEntityException $exception) { + throw new Cloud\RemoveWorkflowFailedException('Something went wrong while removing the workflow. It seems the data you sent was invalid, please check your input.', previous: $exception); + } catch (Api\Exception\SoftDeleteWorkflowItemNotFoundException $exception) { + throw new Cloud\RemoveWorkflowFailedException('Something went wrong while removing the workflow. It seems the data you want to delete do not exists.', previous: $exception); + } + return new Cloud\Event\Workflow\WorkflowRemoved($result->getId()); } } diff --git a/src/Cloud/RemoveWorkflowFailedException.php b/src/Cloud/RemoveWorkflowFailedException.php new file mode 100644 index 00000000..1221647a --- /dev/null +++ b/src/Cloud/RemoveWorkflowFailedException.php @@ -0,0 +1,7 @@ +