Skip to content

Commit

Permalink
entity service node
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaukutsu committed Dec 16, 2023
1 parent 591673f commit 7442af5
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 32 deletions.
36 changes: 36 additions & 0 deletions src/EntityNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace kuaukutsu\poc\task;

use Psr\Container\ContainerExceptionInterface;
use kuaukutsu\poc\task\service\StageCommand;
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\service\TaskCommand;
use kuaukutsu\poc\task\service\TaskQuery;

interface EntityNode
{
public function label(): string;

/**
* @throws ContainerExceptionInterface
*/
public function getTaskQuery(): TaskQuery;

/**
* @throws ContainerExceptionInterface
*/
public function getTaskCommand(): TaskCommand;

/**
* @throws ContainerExceptionInterface
*/
public function getStageQuery(): StageQuery;

/**
* @throws ContainerExceptionInterface
*/
public function getStageCommand(): StageCommand;
}
34 changes: 30 additions & 4 deletions src/TaskBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace kuaukutsu\poc\task;

use LogicException;
use DI\FactoryInterface;
use Psr\Container\ContainerExceptionInterface;
use kuaukutsu\poc\task\exception\BuilderException;
use kuaukutsu\poc\task\service\TaskCreator;

final class TaskBuilder
{
public function __construct(private readonly TaskCreator $factory)
public function __construct(private readonly FactoryInterface $factory)
{
}

Expand All @@ -26,10 +28,34 @@ public function create(string $title, EntityWrapper ...$stages): TaskDraft
* @throws BuilderException
* @throws LogicException
*/
public function build(TaskDraft $draft, ?TaskStageContext $context = null): EntityTask
public function build(EntityNode $node, TaskDraft $draft, ?TaskStageContext $context = null): EntityTask
{
try {
$creator = $this->factoryCreatorByNode($node);
} catch (ContainerExceptionInterface $exception) {
throw new BuilderException("[{$draft->getTitle()}] TaskBuilder dependency resolv failed.", $exception);
}

return $context === null
? $this->factory->create($draft)
: $this->factory->createFromContext($draft, $context);
? $creator->create($draft)
: $creator->createFromContext($draft, $context);
}

/**
* @throws ContainerExceptionInterface
*/
private function factoryCreatorByNode(EntityNode $node): TaskCreator
{
/**
* @var TaskCreator
*/
return $this->factory->make(
TaskCreator::class,
[
'taskQuery' => $node->getTaskQuery(),
'taskCommand' => $node->getTaskCommand(),
'stageCommand' => $node->getStageCommand(),
]
);
}
}
17 changes: 15 additions & 2 deletions src/service/TaskCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use kuaukutsu\poc\task\dto\TaskModelCreate;
use kuaukutsu\poc\task\dto\TaskModel;
use kuaukutsu\poc\task\exception\BuilderException;
use kuaukutsu\poc\task\exception\NotFoundException;
use kuaukutsu\poc\task\state\TaskStateReady;
use kuaukutsu\poc\task\state\TaskStateRelation;
use kuaukutsu\poc\task\handler\TaskFactory;
Expand All @@ -25,7 +26,6 @@ public function __construct(
private readonly TaskCommand $taskCommand,
private readonly StageCommand $stageCommand,
private readonly TaskFactory $factory,
private readonly TaskDestroyer $destroyer,
) {
}

Expand Down Expand Up @@ -95,7 +95,7 @@ private function save(TaskDraft $draft): TaskModel
);
}
} catch (Exception $exception) {
$this->destroyer->purge(
$this->purge(
new EntityUuid($task->uuid)
);

Expand All @@ -122,4 +122,17 @@ private function validateDraft(TaskDraft $draft): void
);
}
}

private function purge(EntityUuid $uuid): void
{
try {
$this->stageCommand->removeByTask($uuid);
} catch (NotFoundException) {
}

try {
$this->taskCommand->remove($uuid);
} catch (NotFoundException) {
}
}
}
7 changes: 6 additions & 1 deletion tests/ProcessingCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
use kuaukutsu\poc\task\service\TaskExecutor;
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\EntityWrapper;
use kuaukutsu\poc\task\EntityNode;
use kuaukutsu\poc\task\EntityUuid;
use kuaukutsu\poc\task\EntityTask;
use kuaukutsu\poc\task\TaskManagerOptions;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\service\StubNode;
use kuaukutsu\poc\task\tests\stub\TestStageStub;

final class ProcessingCheckTest extends TestCase
Expand All @@ -25,6 +27,8 @@ final class ProcessingCheckTest extends TestCase

private readonly EntityTask $task;

private readonly EntityNode $node;

private readonly TaskQuery $taskQuery;

private readonly StageQuery $stageQuery;
Expand Down Expand Up @@ -52,6 +56,7 @@ public function __construct(string $name)
$this->processing = self::get(TaskProcessing::class);
$this->taskExecutor = self::get(TaskExecutor::class);
$this->builder = self::get(TaskBuilder::class);
$this->node = self::get(StubNode::class);

$this->options = new TaskManagerOptions(
bindir: __DIR__ . '/bin',
Expand Down Expand Up @@ -100,7 +105,7 @@ class: TestStageStub::class,
),
);

$this->task = $this->builder->build($draft);
$this->task = $this->builder->build($this->node, $draft);
}

protected function tearDown(): void
Expand Down
7 changes: 6 additions & 1 deletion tests/ProcessingDelayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
use kuaukutsu\poc\task\state\TaskStateInterface;
use kuaukutsu\poc\task\service\TaskDestroyer;
use kuaukutsu\poc\task\EntityWrapper;
use kuaukutsu\poc\task\EntityNode;
use kuaukutsu\poc\task\EntityUuid;
use kuaukutsu\poc\task\EntityTask;
use kuaukutsu\poc\task\TaskManagerOptions;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\service\StubNode;
use kuaukutsu\poc\task\tests\stub\TestStageStub;

final class ProcessingDelayTest extends TestCase
Expand All @@ -28,6 +30,8 @@ final class ProcessingDelayTest extends TestCase

private readonly EntityTask $task;

private readonly EntityNode $node;

private readonly StageHandler $handler;

private readonly TaskProcessing $processing;
Expand All @@ -49,6 +53,7 @@ public function __construct(string $name)
$this->processing = self::get(TaskProcessing::class);
$this->builder = self::get(TaskBuilder::class);
$this->handler = self::get(StageHandler::class);
$this->node = self::get(StubNode::class);

$this->options = new TaskManagerOptions(
bindir: __DIR__ . '/bin',
Expand Down Expand Up @@ -104,7 +109,7 @@ class: TestStageStub::class,
),
);

$this->task = $this->builder->build($draft);
$this->task = $this->builder->build($this->node, $draft);
}

protected function tearDown(): void
Expand Down
6 changes: 6 additions & 0 deletions tests/ProcessingPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
use kuaukutsu\poc\task\service\TaskDestroyer;
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\EntityWrapper;
use kuaukutsu\poc\task\EntityNode;
use kuaukutsu\poc\task\EntityUuid;
use kuaukutsu\poc\task\EntityTask;
use kuaukutsu\poc\task\TaskManagerOptions;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\service\StubNode;
use kuaukutsu\poc\task\tests\stub\TestContextResponseStageStub;
use kuaukutsu\poc\task\tests\stub\TestHandlerStageStub;

Expand All @@ -35,6 +37,8 @@ final class ProcessingPromiseTest extends TestCase

private readonly EntityTask $task;

private readonly EntityNode $node;

private readonly TaskQuery $taskQuery;

private readonly StageQuery $stageQuery;
Expand Down Expand Up @@ -62,6 +66,7 @@ public function __construct(string $name)
$this->processing = self::get(TaskProcessing::class);
$this->builder = self::get(TaskBuilder::class);
$this->handler = self::get(StageHandler::class);
$this->node = self::get(StubNode::class);

$this->options = new TaskManagerOptions(
bindir: __DIR__ . '/bin',
Expand Down Expand Up @@ -215,6 +220,7 @@ public function testLoadingPromise(): void
protected function setUp(): void
{
$this->task = $this->builder->build(
$this->node,
$this->builder->create(
'task test promise',
new EntityWrapper(
Expand Down
7 changes: 6 additions & 1 deletion tests/ProcessingSkipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use kuaukutsu\poc\task\service\TaskExecutor;
use kuaukutsu\poc\task\service\StageQuery;
use kuaukutsu\poc\task\EntityWrapper;
use kuaukutsu\poc\task\EntityNode;
use kuaukutsu\poc\task\EntityUuid;
use kuaukutsu\poc\task\EntityTask;
use kuaukutsu\poc\task\TaskManagerOptions;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\service\StubNode;
use kuaukutsu\poc\task\tests\stub\TestStageStub;

final class ProcessingSkipTest extends TestCase
Expand All @@ -27,6 +29,8 @@ final class ProcessingSkipTest extends TestCase

private readonly EntityTask $task;

private readonly EntityNode $node;

private readonly TaskQuery $taskQuery;

private readonly StageQuery $stageQuery;
Expand Down Expand Up @@ -54,6 +58,7 @@ public function __construct(string $name)
$this->processing = self::get(TaskProcessing::class);
$this->taskExecutor = self::get(TaskExecutor::class);
$this->builder = self::get(TaskBuilder::class);
$this->node = self::get(StubNode::class);

$this->options = new TaskManagerOptions(
bindir: __DIR__ . '/bin',
Expand Down Expand Up @@ -105,7 +110,7 @@ class: TestStageStub::class,
)
);

$this->task = $this->builder->build($draft);
$this->task = $this->builder->build($this->node, $draft);
}

protected function tearDown(): void
Expand Down
4 changes: 3 additions & 1 deletion tests/ProcessingTerminateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use kuaukutsu\poc\task\EntityUuid;
use kuaukutsu\poc\task\TaskManagerOptions;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\service\StubNode;

final class ProcessingTerminateTest extends TestCase
{
Expand Down Expand Up @@ -84,7 +85,8 @@ public function testTerminate(): void
protected function setUp(): void
{
$this->task = $this->generateTask(
self::get(TaskBuilder::class)
self::get(StubNode::class),
self::get(TaskBuilder::class),
);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/ProcessingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use kuaukutsu\poc\task\EntityTask;
use kuaukutsu\poc\task\TaskManagerOptions;
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\tests\service\StubNode;

final class ProcessingTest extends TestCase
{
Expand Down Expand Up @@ -97,7 +98,8 @@ public function testLoadTaskProcess(): void
protected function setUp(): void
{
$this->task = $this->generateTask(
self::get(TaskBuilder::class)
self::get(StubNode::class),
self::get(TaskBuilder::class),
);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/StageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use kuaukutsu\poc\task\TaskBuilder;
use kuaukutsu\poc\task\EntityTask;
use kuaukutsu\poc\task\EntityUuid;
use kuaukutsu\poc\task\tests\service\StubNode;

final class StageServiceTest extends TestCase
{
Expand Down Expand Up @@ -142,7 +143,8 @@ public function testContextFactory(): void
protected function setUp(): void
{
$this->task = $this->generateTask(
self::get(TaskBuilder::class)
self::get(StubNode::class),
self::get(TaskBuilder::class),
);

$this->stage = $this->query
Expand Down
Loading

0 comments on commit 7442af5

Please sign in to comment.