Skip to content

Commit

Permalink
close #31
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Oct 17, 2023
1 parent f9ff28e commit 20cfc10
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 168 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
![Chevere](chevere.svg)

[![Build](https://img.shields.io/github/actions/workflow/status/chevere/workflow/test.yml?branch=0.8&style=flat-square)](https://github.com/chevere/workflow/actions)
[![Build](https://img.shields.io/github/actions/workflow/status/chevere/workflow/test.yml?branch=0.9&style=flat-square)](https://github.com/chevere/workflow/actions)
![Code size](https://img.shields.io/github/languages/code-size/chevere/workflow?style=flat-square)
[![Apache-2.0](https://img.shields.io/github/license/chevere/workflow?style=flat-square)](LICENSE)
[![PHPStan](https://img.shields.io/badge/PHPStan-level%209-blueviolet?style=flat-square)](https://phpstan.org/)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Fworkflow%2F0.8)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/workflow/0.8)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchevere%2Fworkflow%2F0.9)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/workflow/0.9)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=chevere_workflow&metric=alert_status)](https://sonarcloud.io/dashboard?id=chevere_workflow)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=chevere_workflow&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chevere_workflow)
Expand Down Expand Up @@ -40,26 +40,27 @@ Create a Workflow by passing named jobs.
use function Chevere\Workflow\workflow;
use function Chevere\Workflow\sync;
use function Chevere\Workflow\async;
use function Chevere\Workflow\reference;
use function Chevere\Workflow\variable;

$workflow = workflow(
thumb: async(
ImageResize::class,
new ImageResize(),
file: variable('image'),
fit: 'thumbnail',
),
poster: async(
ImageResize::class,
new ImageResize(),
file: variable('file'),
fit: 'poster',
),
storeThumb: async(
StoreFile::class,
new StoreFile(),
file: reference('thumb', 'out'),
path: variable('savePath'),
),
storePoster: async(
StoreFile::class,
new StoreFile(),
file: reference('poster', 'out'),
path: variable('savePath'),
)
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/JobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Chevere\Workflow\Interfaces;

use Chevere\Action\Interfaces\ActionNameInterface;
use Chevere\Action\Interfaces\ActionInterface;
use Chevere\DataStructure\Interfaces\VectorInterface;

/**
Expand All @@ -37,7 +37,7 @@ public function withIsSync(bool $flag = true): self;

public function withDepends(string ...$jobs): self;

public function actionName(): ActionNameInterface;
public function action(): ActionInterface;

/**
* @return array<string, mixed>
Expand Down
14 changes: 7 additions & 7 deletions src/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Chevere\Workflow;

use Chevere\Action\Interfaces\ActionNameInterface;
use Chevere\Action\Interfaces\ActionInterface;
use Chevere\DataStructure\Interfaces\VectorInterface;
use Chevere\DataStructure\Vector;
use Chevere\Parameter\Interfaces\ParameterInterface;
Expand Down Expand Up @@ -49,13 +49,13 @@ final class Job implements JobInterface
private VectorInterface $runIf;

public function __construct(
private ActionNameInterface $actionName,
private ActionInterface $action,
private bool $isSync = false,
mixed ...$argument
) {
$this->runIf = new Vector();
$this->dependencies = new Vector();
$this->parameters = getParameters($actionName->__toString());
$this->parameters = getParameters($action::class);
$this->arguments = [];
$this->setArguments(...$argument);
}
Expand Down Expand Up @@ -104,9 +104,9 @@ public function withDepends(string ...$jobs): JobInterface
return $new;
}

public function actionName(): ActionNameInterface
public function action(): ActionInterface
{
return $this->actionName;
return $this->action;
}

public function arguments(): array
Expand Down Expand Up @@ -149,7 +149,7 @@ private function setArguments(mixed ...$argument): void
throw new BadMethodCallException(
message('Missing argument(s) [%arguments%] for %action%')
->withCode('%arguments%', implode(', ', $missing))
->withCode('%action%', $this->actionName->__toString())
->withCode('%action%', $this->action::class)
);
}
$this->arguments = $values;
Expand All @@ -170,7 +170,7 @@ private function assertArgumentsCount(array $arguments): void

throw new ArgumentCountError(
message('%symbol% requires %countRequired% argument(s) %parameters%')
->withCode('%symbol%', $this->actionName->__toString() . '::run')
->withCode('%symbol%', $this->action::class . '::run')
->withCode('%countRequired%', strval($countRequired))
->withCode('%parameters%', $parameters)
);
Expand Down
36 changes: 22 additions & 14 deletions src/Jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ private function putAdded(JobInterface ...$job): void

private function storeReferences(string $job, JobInterface $item): void
{
$action = $item->actionName()->__toString();
if ($action::acceptResponse() instanceof ParametersAccessInterface) {
foreach ($action::acceptResponse()->parameters() as $key => $parameter) {
$action = $item->action();
$acceptResponse = $action::acceptResponse();
if ($acceptResponse instanceof ParametersAccessInterface) {
foreach ($acceptResponse->parameters() as $key => $parameter) {
$this->references = $this->references
->withPut(
strval(response($job, $key)),
Expand All @@ -152,16 +153,16 @@ private function storeReferences(string $job, JobInterface $item): void
$this->references = $this->references
->withPut(
strval(response($job)),
$action::acceptResponse(),
$acceptResponse,
);
}
}

private function handleArguments(string $job, JobInterface $item): void
{
foreach ($item->arguments() as $argument => $value) {
$action = $item->actionName()->__toString();
$parameter = getParameters($action)->get($argument);
$action = $item->action();
$parameter = getParameters($action::class)->get($argument);
$collection = match (true) {
$value instanceof VariableInterface => 'variables',
$value instanceof ResponseReferenceInterface => 'references',
Expand Down Expand Up @@ -202,7 +203,7 @@ private function mapParameter(
/** @var JobInterface $referenceJob */
$referenceJob = $this->map->get($value->job());
/** @var ParameterInterface $acceptResponse */
$acceptResponse = $referenceJob->actionName()->__toString()::acceptResponse();
$acceptResponse = $referenceJob->action()::acceptResponse();
if ($value->key() !== null) {
/** @var ParametersAccessInterface $acceptResponse */
$acceptResponse->parameters()->get($value->key());
Expand Down Expand Up @@ -253,14 +254,21 @@ private function handleRunIfReference(mixed $runIf): void
if (! $runIf instanceof ResponseReferenceInterface) {
return;
}
$action = $this->get($runIf->job())->actionName()->__toString();
$parameter = $action::acceptResponse()->parameters()->get($runIf->key());
if ($parameter->type()->primitive() !== 'boolean') {
throw new TypeError(
message('Reference %reference% must be of type boolean')
->withCode('%reference%', $runIf->__toString())
);
$action = $this->get($runIf->job())->action();
$parameter = $action::acceptResponse();
if ($runIf->key() !== null
&& $parameter instanceof ParametersAccessInterface
) {
$parameter = $parameter->parameters()->get($runIf->key());
}
if ($parameter->type()->primitive() === 'boolean') {
return;
}

throw new TypeError(
message('Reference %reference% must be of type boolean')
->withCode('%reference%', strval($runIf))
);
}

private function handleRunIfVariable(string $name, mixed $runIf): void
Expand Down
4 changes: 1 addition & 3 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ public function withRunJob(string $name): RunnerInterface
}
}
$arguments = $new->getJobArguments($job);
$action = $job->actionName()->__toString();
/** @var ActionInterface $action */
$action = new $action();
$action = $job->action();
$response = $new->getActionResponse($action, $arguments);
$new->addJobResponse($name, $response);

Expand Down
4 changes: 2 additions & 2 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public function getJobResponseParameter(string $job): ParameterInterface

private function putParameters(string $name, JobInterface $job): void
{
$action = $job->actionName()->__toString();
$parameters = getParameters($action);
$action = $job->action();
$parameters = getParameters($action::class);
$this->provided = $this->provided->withPut($name, $action::acceptResponse());
foreach ($job->arguments() as $argument => $value) {
try {
Expand Down
15 changes: 3 additions & 12 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Workflow;

use Chevere\Action\ActionName;
use Chevere\Action\Interfaces\ActionInterface;
use Chevere\Container\Container;
use Chevere\Workflow\Interfaces\JobInterface;
Expand All @@ -40,29 +39,21 @@ function workflow(JobInterface ...$job): WorkflowInterface
/**
* Creates a synchronous job for the given action and arguments.
*
* @param class-string<ActionInterface> $action
* @param mixed ...$argument Action arguments for its run method (raw, reference or variable)
*/
function sync(string $action, mixed ...$argument): JobInterface
function sync(ActionInterface $action, mixed ...$argument): JobInterface
{
$action = new ActionName($action);

return new Job($action, true, ...$argument);
}

/**
* Creates an asynchronous job for the given action and arguments.
*
* @param class-string<ActionInterface> $action
* @param mixed ...$argument Action arguments for its run method (raw, reference or variable)
*/
function async(string $action, mixed ...$argument): JobInterface
function async(ActionInterface $action, mixed ...$argument): JobInterface
{
return new Job(
new ActionName($action),
false,
...$argument
);
return new Job($action, false, ...$argument);
}

/**
Expand Down
13 changes: 6 additions & 7 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Chevere\Tests;

use Chevere\Action\ActionName;
use Chevere\Tests\src\TestActionNoParamsIntegerResponse;
use Chevere\Workflow\Job;
use Chevere\Workflow\Jobs;
Expand All @@ -33,17 +32,17 @@ public function testFunctionWorkflow(): void

public function testFunctionSync(): void
{
$actionName = new ActionName(TestActionNoParamsIntegerResponse::class);
$job = sync($actionName->__toString());
$alt = new Job($actionName, true);
$action = new TestActionNoParamsIntegerResponse();
$job = sync($action);
$alt = new Job($action, true);
$this->assertEquals($alt, $job);
}

public function testFunctionAsync(): void
{
$actionName = new ActionName(TestActionNoParamsIntegerResponse::class);
$job = async($actionName->__toString());
$alt = new Job($actionName, false);
$action = new TestActionNoParamsIntegerResponse();
$job = async($action);
$alt = new Job($action, false);
$this->assertEquals($alt, $job);
}
}
4 changes: 3 additions & 1 deletion tests/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ public function testWithPutDigit(): void

private function getJob(): JobInterface
{
return async(TestActionNoParams::class);
return async(
new TestActionNoParams()
);
}
}
Loading

0 comments on commit 20cfc10

Please sign in to comment.