Skip to content

Commit

Permalink
Started to create classes to send workflow configurations to the cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Oct 23, 2023
1 parent 247b399 commit c6127f4
Show file tree
Hide file tree
Showing 12 changed files with 508 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Cloud/Command/Workflow/DeclareWorkflowCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\Command\Workflow;

use Kiboko\Component\Satellite\Cloud\Command\Command;
use Kiboko\Component\Satellite\Cloud\DTO;

final class DeclareWorkflowCommand implements Command
{
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\OrganizationId $organizationId,
public DTO\WorkspaceId $project,
) {}
}
1 change: 1 addition & 0 deletions src/Cloud/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static function withStandardHandlers(Client $client): self
Satellite\Cloud\Command\Pipeline\AddBeforePipelineStepCommand::class => new Satellite\Cloud\Handler\Pipeline\AddBeforePipelineStepCommandHandler($client),
Satellite\Cloud\Command\Pipeline\ReplacePipelineStepCommand::class => new Satellite\Cloud\Handler\Pipeline\ReplacePipelineStepCommandHandler($client),
Satellite\Cloud\Command\Pipeline\RemovePipelineStepCommand::class => new Satellite\Cloud\Handler\Pipeline\RemovePipelineStepCommandHandler($client),
Satellite\Cloud\Command\Workflow\DeclareWorkflowCommand::class => new Satellite\Cloud\Handler\Workflow\DeclareWorkflowCommandHandler($client),
]);
}

Expand Down
22 changes: 22 additions & 0 deletions src/Cloud/DTO/JobCode.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 JobCode implements \Stringable
{
public function __construct(
private string $reference,
) {}

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

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

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO;

use Kiboko\Component\Satellite\Cloud\DTO\Workflow\JobInterface;

readonly class JobList implements \Countable, \IteratorAggregate
{
/** @var list<JobInterface> */
private array $jobs;

public function __construct(
JobInterface ...$job,
) {
$this->jobs = $job;
}

public function getIterator(): \Traversable
{
$jobs = $this->jobs;
usort($jobs, fn (JobInterface $left, JobInterface $right) => $left->order <=> $right->order);

Check failure on line 23 in src/Cloud/DTO/JobList.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Kiboko\Component\Satellite\Cloud\DTO\Workflow\JobInterface::$order.

Check failure on line 23 in src/Cloud/DTO/JobList.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Kiboko\Component\Satellite\Cloud\DTO\Workflow\JobInterface::$order.

return new \ArrayIterator($jobs);
}

public function codes(): array
{
$jobs = $this->jobs;
usort($jobs, fn (JobInterface $left, JobInterface $right) => $left->order <=> $right->order);

Check failure on line 31 in src/Cloud/DTO/JobList.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Kiboko\Component\Satellite\Cloud\DTO\Workflow\JobInterface::$order.

Check failure on line 31 in src/Cloud/DTO/JobList.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Kiboko\Component\Satellite\Cloud\DTO\Workflow\JobInterface::$order.

return array_map(fn (JobInterface $job) => $job->code->asString(), $jobs);

Check failure on line 33 in src/Cloud/DTO/JobList.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Kiboko\Component\Satellite\Cloud\DTO\Workflow\JobInterface::$code.
}

public function get(string $code): JobInterface
{
foreach ($this->jobs as $job) {
if ($job->code->asString() === $code) {

Check failure on line 39 in src/Cloud/DTO/JobList.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Kiboko\Component\Satellite\Cloud\DTO\Workflow\JobInterface::$code.
return $job;
}
}

throw new \OutOfBoundsException('There was no job found matching the provided code');
}

public function count(): int
{
return \count($this->jobs);
}

public function map(callable $callback): array
{
return array_map($callback, $this->jobs);
}
}
18 changes: 18 additions & 0 deletions src/Cloud/DTO/Workflow/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO\Workflow;

use Kiboko\Component\Satellite\Cloud\DTO\JobCode;

final readonly class Action implements JobInterface
{
public function __construct(
public string $label,
public JobCode $code,
public array $configuration,
public int $order,
) {
}
}
12 changes: 12 additions & 0 deletions src/Cloud/DTO/Workflow/JobInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO\Workflow;

use Kiboko\Component\Satellite\Cloud\DTO\JobCode;

interface JobInterface
{

}
19 changes: 19 additions & 0 deletions src/Cloud/DTO/Workflow/Pipeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO\Workflow;

use Kiboko\Component\Satellite\Cloud\DTO\JobCode;
use Kiboko\Component\Satellite\Cloud\DTO\StepList;

final readonly class Pipeline implements JobInterface
{
public function __construct(
public string $label,
public JobCode $code,
public StepList $stepList,
public int $order,
) {
}
}
22 changes: 22 additions & 0 deletions src/Cloud/DTO/WorkflowId.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 WorkflowId implements \Stringable
{
public function __construct(
private string $reference,
) {}

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

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

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\Event\Workflow;

final readonly class WorkflowDeclared
{
public function __construct(
private string $id,
) {}

public function getId(): string
{
return $this->id;
}
}
68 changes: 68 additions & 0 deletions src/Cloud/Handler/Workflow/DeclareWorkflowCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\Handler\Workflow;

use Gyroscops\Api;
use Kiboko\Component\Satellite\Cloud;
use Kiboko\Component\Satellite\Cloud\DTO\Probe;
use Kiboko\Component\Satellite\Cloud\DTO\PSR4AutoloadConfig;
use Kiboko\Component\Satellite\Cloud\DTO\Step;

final readonly class DeclareWorkflowCommandHandler
{
public function __construct(
private Api\Client $client,
) {}

/** TODO: update this method */
public function __invoke(Cloud\Command\Pipeline\DeclarePipelineCommand $command): Cloud\Event\PipelineDeclared
{
try {
/** @var \stdClass $result */
$result = $this->client->declarePipelinePipelineCollection(
(new Api\Model\PipelineDeclarePipelineCommandInput())
->setLabel($command->label)
->setCode($command->code)
->setSteps($command->steps->map(
fn (Step $step) => (new Api\Model\StepInput())
->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)
)),
);
} 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);
}
}
Loading

0 comments on commit c6127f4

Please sign in to comment.