Skip to content

Commit

Permalink
Refactored and simplified the code
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Oct 26, 2023
1 parent 51c3dc5 commit 14c37c5
Show file tree
Hide file tree
Showing 26 changed files with 240 additions and 276 deletions.
3 changes: 1 addition & 2 deletions src/Cloud/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Kiboko\Component\Satellite\Cloud;

use DateTimeInterface;
use Gyroscops\Api;
use Kiboko\Component\Satellite\Cloud\DTO\OrganizationId;
use Kiboko\Component\Satellite\Cloud\DTO\WorkspaceId;
Expand Down Expand Up @@ -167,7 +166,7 @@ public function token(string $url): string
throw new AccessDeniedException('There is no available token to authenticate to the service.');
}

$date = \DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $this->configuration[$url]['date']);
$date = \DateTimeImmutable::createFromFormat(\DateTimeInterface::RFC3339_EXTENDED, $this->configuration[$url]['date']);
if ($date <= new \DateTimeImmutable('-1 hour')) {
throw new AccessDeniedException('The stored token has expired, you need a fresh token to authenticate to the service.');
}
Expand Down
5 changes: 1 addition & 4 deletions src/Cloud/Command/Pipeline/DeclarePipelineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public function __construct(
public string $code,
public string $label,
public DTO\StepList $steps,
public DTO\Autoload $autoload,
public DTO\PackageList $packages,
public DTO\RepositoryList $repositories,
public DTO\AuthList $auths,
public DTO\Composer $composer,
public DTO\OrganizationId $organizationId,
public DTO\WorkspaceId $project,
) {}
Expand Down
5 changes: 1 addition & 4 deletions src/Cloud/Command/Workflow/DeclareWorkflowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public function __construct(
public string $code,
public string $label,
public DTO\JobList $jobs,
public DTO\Autoload $autoload,
public DTO\PackageList $packages,
public DTO\RepositoryList $repositories,
public DTO\AuthList $auths,
public DTO\Composer $composer,
public DTO\OrganizationId $organizationId,
public DTO\WorkspaceId $project,
) {}
Expand Down
35 changes: 35 additions & 0 deletions src/Cloud/DTO/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO;

final readonly class Composer
{
public function __construct(
private Autoload $autoload,
private PackageList $packages,
private RepositoryList $repositories,
private AuthList $auths,
) {}

public function autoload(): Autoload
{
return $this->autoload;
}

public function packages(): PackageList
{
return $this->packages;
}

public function repositories(): RepositoryList
{
return $this->repositories;
}

public function auths(): AuthList
{
return $this->auths;
}
}
26 changes: 4 additions & 22 deletions src/Cloud/DTO/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

namespace Kiboko\Component\Satellite\Cloud\DTO;

final readonly class Pipeline implements PipelineInterface
final readonly class Pipeline implements SatelliteInterface, PipelineInterface
{
public function __construct(
private string $label,
private string $code,
private StepList $steps,
private Autoload $autoload,
private PackageList $packages,
private RepositoryList $repositories,
private AuthList $auths,
private Composer $composer,
) {}

public function code(): string
Expand All @@ -31,23 +28,8 @@ public function steps(): StepList
return $this->steps;
}

public function autoload(): Autoload
public function composer(): Composer
{
return $this->autoload;
}

public function packages(): PackageList
{
return $this->packages;
}

public function repositories(): RepositoryList
{
return $this->repositories;
}

public function auths(): AuthList
{
return $this->auths;
return $this->composer;
}
}
8 changes: 0 additions & 8 deletions src/Cloud/DTO/PipelineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,4 @@ public function code(): string;
public function label(): string;

public function steps(): StepList;

public function autoload(): Autoload;

public function packages(): PackageList;

public function repositories(): RepositoryList;

public function auths(): AuthList;
}
22 changes: 22 additions & 0 deletions src/Cloud/DTO/SatelliteId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO;

final readonly class SatelliteId implements \Stringable
{
public function __construct(
private string $reference,
) {}

public function asString(): string
{
return $this->reference;
}

public function __toString(): string
{
return $this->reference;
}
}
14 changes: 14 additions & 0 deletions src/Cloud/DTO/SatelliteInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO;

interface SatelliteInterface
{
public function code(): string;

public function label(): string;

public function composer(): Composer;
}
30 changes: 6 additions & 24 deletions src/Cloud/DTO/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

namespace Kiboko\Component\Satellite\Cloud\DTO;

final readonly class Workflow implements WorkflowInterface
final readonly class Workflow implements SatelliteInterface, WorkflowInterface
{
public function __construct(
private string $label,
private string $code,
private JobList $jobs,
private Autoload $autoload,
private PackageList $packages,
private RepositoryList $repositories,
private AuthList $auths,
private Composer $composer,
) {}

public function code(): string
Expand All @@ -26,28 +23,13 @@ public function label(): string
return $this->label;
}

public function jobs(): JobList
public function composer(): Composer
{
return $this->jobs;
return $this->composer;
}

public function autoload(): Autoload
{
return $this->autoload;
}

public function packages(): PackageList
{
return $this->packages;
}

public function repositories(): RepositoryList
{
return $this->repositories;
}

public function auths(): AuthList
public function jobs(): JobList
{
return $this->auths;
return $this->jobs;
}
}
8 changes: 0 additions & 8 deletions src/Cloud/DTO/WorkflowInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,4 @@ public function code(): string;
public function label(): string;

public function jobs(): JobList;

public function autoload(): Autoload;

public function packages(): PackageList;

public function repositories(): RepositoryList;

public function auths(): AuthList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public function __invoke(Cloud\Command\Pipeline\AddAfterPipelineStepCommand $com
throw new Cloud\AddAfterPipelineStepFailedException('Something went wrong while trying to add a new step after an existing pipeline step. It seems the data you sent was invalid, please check your input.', previous: $exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\AddAfterPipelineStepFailedException('Something went wrong while trying to add a new step after an existing pipeline step.');
}

return new Cloud\Event\AddedAfterPipelineStep($result->id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public function __invoke(Cloud\Command\Pipeline\AddBeforePipelineStepCommand $co
throw new Cloud\AddBeforePipelineStepFailedException('Something went wrong while trying to add a new step before an existing pipeline step. It seems the data you sent was invalid, please check your input.', previous: $exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\AddBeforePipelineStepFailedException('Something went wrong while trying to add a new step before an existing pipeline step.');
}

return new Cloud\Event\AddedBeforePipelineStep($result->id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public function __invoke(Cloud\Command\Pipeline\AddPipelineComposerPSR4AutoloadC
throw new Cloud\AddPipelineComposerPSR4AutoloadFailedException('Something went wrong while trying to add PSR4 autoloads into the pipeline. It seems the data you sent was invalid, please check your input.', previous: $exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\AddPipelineComposerPSR4AutoloadFailedException('Something went wrong while trying to add PSR4 autoloads into the pipeline.');
}

return new Cloud\Event\AddedPipelineComposerPSR4Autoload($result->id, $result->namespace, $result->paths);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public function __invoke(Cloud\Command\Pipeline\AddPipelineStepProbeCommand $com
throw new Cloud\AddPipelineStepProbeFailedException('Something went wrong while trying to add a probe into an existing pipeline step. It seems the data you sent was invalid, please check your input.', previous: $exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\AddPipelineStepProbeFailedException('Something went wrong while trying to add a probe into an existing pipeline step.');
}

return new Cloud\Event\AddedPipelineStepProbe($result->id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public function __invoke(Cloud\Command\Pipeline\AppendPipelineStepCommand $comma
throw new Cloud\AppendPipelineStepFailedException('Something went wrong while trying to append a pipeline step. It seems the data you sent was invalid, please check your input.', previous: $exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\AppendPipelineStepFailedException('Something went wrong while trying to append a pipeline step.');
}

return new Cloud\Event\AppendedPipelineStep($result->id);
}
}
5 changes: 0 additions & 5 deletions src/Cloud/Handler/Pipeline/CompilePipelineCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public function __invoke(Cloud\Command\Pipeline\CompilePipelineCommand $command)
throw new Cloud\CompilePipelineFailedException('Something went wrong while trying to compile the pipeline. It seems the data you sent was invalid, please check your input.', previous: $exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\CompilePipelineFailedException('Something went wrong while trying to compile the pipeline.');
}

return new Cloud\Event\CompiledPipeline($result->id);
}
}
44 changes: 21 additions & 23 deletions src/Cloud/Handler/Pipeline/DeclarePipelineCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,41 @@ public function __invoke(Cloud\Command\Pipeline\DeclarePipelineCommand $command)
->setLabel($command->label)
->setCode($command->code)
->setSteps($command->steps->map(
fn (Step $step) => (new Api\Model\StepInput())
fn (Step $step) => (new Api\Model\Step())
->setCode((string) $step->code)
->setLabel($step->label)
->setConfiguration($step->config)
->setProbes($step->probes->map(
fn (Probe $probe) => (new Api\Model\Probe())->setCode($probe->code)->setLabel($probe->label))
)
))
->setAutoloads($command->autoload->map(
fn (PSR4AutoloadConfig $autoloadConfig) => (new Api\Model\AutoloadInput())
->setNamespace($autoloadConfig->namespace)
->setPaths($autoloadConfig->paths)
))
->setPackages($command->packages->transform())
->setAuths($command->auths->map(
fn (Cloud\DTO\Auth $auth) => (new Api\Model\AddPipelineComposerAuthCommandInput())
->setUrl($auth->url)
->setToken($auth->token)
))
->setRepositories($command->repositories->map(
fn (Cloud\DTO\Repository $repository) => (new Api\Model\AddPipelineComposerRepositoryCommandInput())
->setName($repository->name)
->setType($repository->type)
->setUrl($repository->url)
)),
->setComposer(
(new Api\Model\Composer())
->setAutoloads($command->composer->autoload()->map(
fn (PSR4AutoloadConfig $autoloadConfig) => (new Api\Model\ComposerAutoload())
->setNamespace($autoloadConfig->namespace)
->setPaths($autoloadConfig->paths)
))
->setPackages($command->composer->packages()->transform())
->setAuthentications($command->composer->auths()->map(
fn (Cloud\DTO\Auth $auth) => (new Api\Model\ComposerAuthentication())
->setUrl($auth->url)
->setToken($auth->token)
))
->setRepositories($command->composer->repositories()->map(
fn (Cloud\DTO\Repository $repository) => (new Api\Model\ComposerRepository())
->setName($repository->name)
->setType($repository->type)
->setUrl($repository->url)
))
),
);
} catch (Api\Exception\DeclarePipelinePipelineCollectionBadRequestException $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);
} catch (Api\Exception\DeclarePipelinePipelineCollectionUnprocessableEntityException $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);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\DeclarePipelineFailedException('Something went wrong while declaring the pipeline.');
}

return new Cloud\Event\PipelineDeclared($result->id);
}
}
5 changes: 0 additions & 5 deletions src/Cloud/Handler/Pipeline/RemovePipelineCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public function __invoke(Cloud\Command\Pipeline\RemovePipelineCommand $command):
throw new Cloud\RemovePipelineFailedException('Something went wrong while trying to remove a step from the pipeline. Maybe you are trying to delete a pipeline that never existed or has already been deleted.', previous: $exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\RemovePipelineFailedException('Something went wrong while trying to remove a step from the pipeline.');
}

return new Cloud\Event\RemovedPipeline((string) $command->pipeline);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public function __invoke(Cloud\Command\Pipeline\RemovePipelineStepCommand $comma
throw new Cloud\RemovePipelineStepFailedException('Something went wrong while trying to remove a probe from the step. Maybe you are trying to delete a step that never existed or has already been deleted.', previous: $exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\RemovePipelineStepFailedException('Something went wrong while trying to remove a probe from the step.');
}

return new Cloud\Event\RemovedPipelineStep((string) $command->code);
}
}
Loading

0 comments on commit 14c37c5

Please sign in to comment.