Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task finally handler add params optional #58

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/TaskDraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ final class TaskDraft implements EntityTask
*/
private ?string $finally = null;

/**
* @var array<string, scalar>
*/
private array $params = [];

private TaskStateInterface $state;

private readonly EntityUuid $uuid;
Expand Down Expand Up @@ -56,6 +61,7 @@ public function getOptions(): TaskOptions
return new TaskOptions(
timeout: $this->timeout,
finally: $this->finally,
params: $this->params,
);
}

Expand Down Expand Up @@ -98,15 +104,17 @@ public function setTimeout(float $timeout): self

/**
* @param class-string<EntityFinally> $handler
* @param array<string, scalar> $params Params for implementating EntityFinally
* @throws LogicException not implement the EntityFinally
*/
public function setFinally(string $handler): self
public function setFinally(string $handler, array $params = []): self
{
if (is_a($handler, EntityFinally::class, true) === false) {
throw new LogicException("[$handler] must implement the EntityFinally.");
}

$this->finally = $handler;
$this->params = $params;
return $this;
}
}
13 changes: 8 additions & 5 deletions src/dto/TaskOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@
final readonly class TaskOptions implements EntityArrable
{
/**
* @param float $timeout В секундах.
* @param float $timeout Seconds.
* @param class-string<EntityFinally>|null $finally
* @param array<string, scalar> $params
*/
public function __construct(
public float $timeout,
public ?string $finally = null,
public array $params = [],
) {
}

public function toArray(): array
{
/**
* @var array<string, scalar>
*/
return get_object_vars($this);
return [
'timeout' => $this->timeout,
'finally' => $this->finally,
'params' => $this->params,
];
}
}
10 changes: 5 additions & 5 deletions src/handler/TaskFinallyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace kuaukutsu\poc\task\handler;

use Throwable;
use DI\FactoryInterface;
use DI\DependencyException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Container\ContainerInterface;
use kuaukutsu\poc\task\dto\TaskOptions;
use kuaukutsu\poc\task\state\TaskStateInterface;
use kuaukutsu\poc\task\state\TaskStatePrepare;
Expand All @@ -18,7 +18,7 @@
{
use TaskStatePrepare;

public function __construct(private ContainerInterface $container)
public function __construct(private FactoryInterface $container)
{
}

Expand All @@ -32,7 +32,7 @@ public function handle(string $uuid, TaskOptions $options, TaskStateInterface $s
}

try {
$this->factory($options->finally)->handle($uuid, $state);
$this->factory($options->finally, $options->params)->handle($uuid, $state);
} catch (Throwable) {
}
}
Expand All @@ -41,10 +41,10 @@ public function handle(string $uuid, TaskOptions $options, TaskStateInterface $s
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function factory(string $className): EntityFinally
private function factory(string $className, array $params): EntityFinally
{
/** @var EntityFinally|object $handler */
$handler = $this->container->get($className);
$handler = $this->container->make($className, $params);
if ($handler instanceof EntityFinally) {
return $handler;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/TaskFinallyHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\service\BaseStorage;
use kuaukutsu\poc\task\tests\stub\TestExceptionFinally;
use kuaukutsu\poc\task\tests\stub\TestParamsFinally;
use kuaukutsu\poc\task\tests\stub\TestFinally;
use kuaukutsu\poc\task\tests\stub\TestStageStub;

Expand Down Expand Up @@ -78,6 +79,41 @@ class: TestStageStub::class,
self::assertEmpty($this->storage->get($this->task->getUuid()));
}

public function testParamsHandler(): void
{
$this->task = $this->builder->build(
$this->builder
->create(
'task finally builder',
new EntityWrapper(
class: TestStageStub::class,
params: [
'name' => 'Test initialization.',
],
),
)
->setFinally(
TestParamsFinally::class,
[
'name' => 'prefix',
]
)
);

$this->taskFinallyHandler->handle(
$this->task->getUuid(),
$this->task->getOptions(),
new TaskStateSuccess(
new TaskStateMessage('test finally')
),
);

self::assertEquals('prefixtest finally', $this->storage->get($this->task->getUuid()));

$this->storage->unset($this->task->getUuid());
self::assertEmpty($this->storage->get($this->task->getUuid()));
}

public function testHandlerException(): void
{
$this->task = $this->builder->build(
Expand Down
23 changes: 23 additions & 0 deletions tests/stub/TestParamsFinally.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace kuaukutsu\poc\task\tests\stub;

use kuaukutsu\poc\task\EntityFinally;
use kuaukutsu\poc\task\state\TaskStateInterface;
use kuaukutsu\poc\task\tests\service\BaseStorage;

final readonly class TestParamsFinally implements EntityFinally
{
public function __construct(
public string $name,
private BaseStorage $storage,
) {
}

public function handle(string $uuid, TaskStateInterface $state): void
{
$this->storage->set($uuid, $this->name . $state->getMessage()->message);
}
}
Loading