From 877e393eca9b2bfd761975c4d54b223ad6450ba0 Mon Sep 17 00:00:00 2001 From: Dmitriy Krivopalov Date: Thu, 15 Aug 2024 06:17:53 +0300 Subject: [PATCH 1/2] Task finally handler add params optional --- src/TaskDraft.php | 10 ++++++++- src/dto/TaskOptions.php | 13 ++++++----- src/handler/TaskFinallyHandler.php | 10 ++++----- tests/TaskFinallyHandlerTest.php | 36 ++++++++++++++++++++++++++++++ tests/stub/TestParamsFinally.php | 23 +++++++++++++++++++ 5 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 tests/stub/TestParamsFinally.php diff --git a/src/TaskDraft.php b/src/TaskDraft.php index 67e681c..85767ef 100644 --- a/src/TaskDraft.php +++ b/src/TaskDraft.php @@ -21,6 +21,11 @@ final class TaskDraft implements EntityTask */ private ?string $finally = null; + /** + * @var array + */ + private array $params = []; + private TaskStateInterface $state; private readonly EntityUuid $uuid; @@ -56,6 +61,7 @@ public function getOptions(): TaskOptions return new TaskOptions( timeout: $this->timeout, finally: $this->finally, + params: $this->params, ); } @@ -98,15 +104,17 @@ public function setTimeout(float $timeout): self /** * @param class-string $handler + * @param array $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; } } diff --git a/src/dto/TaskOptions.php b/src/dto/TaskOptions.php index cc99a8f..ffc080f 100644 --- a/src/dto/TaskOptions.php +++ b/src/dto/TaskOptions.php @@ -13,20 +13,23 @@ final readonly class TaskOptions implements EntityArrable { /** - * @param float $timeout В секундах. + * @param float $timeout Seconds. * @param class-string|null $finally + * @param array $params */ public function __construct( public float $timeout, public ?string $finally = null, + public array $params = [], ) { } public function toArray(): array { - /** - * @var array - */ - return get_object_vars($this); + return [ + 'timeout' => $this->timeout, + 'finally' => $this->finally, + 'params' => $this->params, + ]; } } diff --git a/src/handler/TaskFinallyHandler.php b/src/handler/TaskFinallyHandler.php index d716a63..5515c8e 100644 --- a/src/handler/TaskFinallyHandler.php +++ b/src/handler/TaskFinallyHandler.php @@ -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; @@ -18,7 +18,7 @@ { use TaskStatePrepare; - public function __construct(private ContainerInterface $container) + public function __construct(private FactoryInterface $container) { } @@ -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) { } } @@ -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; } diff --git a/tests/TaskFinallyHandlerTest.php b/tests/TaskFinallyHandlerTest.php index cf2db1a..2eb4453 100644 --- a/tests/TaskFinallyHandlerTest.php +++ b/tests/TaskFinallyHandlerTest.php @@ -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; @@ -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( diff --git a/tests/stub/TestParamsFinally.php b/tests/stub/TestParamsFinally.php new file mode 100644 index 0000000..574c21c --- /dev/null +++ b/tests/stub/TestParamsFinally.php @@ -0,0 +1,23 @@ +storage->set($uuid, $this->name . $state->getMessage()->message); + } +} From 4eeaab15f7fe26db57a46a6f831a84c48d68b185 Mon Sep 17 00:00:00 2001 From: Dmitriy Krivopalov Date: Thu, 15 Aug 2024 06:19:45 +0300 Subject: [PATCH 2/2] fix code style --- tests/TaskFinallyHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TaskFinallyHandlerTest.php b/tests/TaskFinallyHandlerTest.php index 2eb4453..cd565bd 100644 --- a/tests/TaskFinallyHandlerTest.php +++ b/tests/TaskFinallyHandlerTest.php @@ -95,7 +95,7 @@ class: TestStageStub::class, ->setFinally( TestParamsFinally::class, [ - 'name' => 'prefix' + 'name' => 'prefix', ] ) );