Skip to content

Commit

Permalink
Merge pull request #146 from duyler/dev
Browse files Browse the repository at this point in the history
Extend handling state suspend and resume
  • Loading branch information
milinsky committed May 9, 2024
2 parents c38ecf7 + 6e07e0a commit a160307
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 81 deletions.
5 changes: 4 additions & 1 deletion src/Contract/State/MainResumeStateHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use Duyler\ActionBus\State\Service\StateMainResumeService;
use Duyler\ActionBus\State\StateContext;
use Duyler\ActionBus\State\Suspend;

interface MainResumeStateHandlerInterface extends StateHandlerInterface
{
public function handle(StateMainResumeService $stateService, StateContext $context): mixed;
public function handle(StateMainResumeService $stateService, StateContext $context): void;

public function observed(Suspend $suspend, StateContext $context): bool;
}
4 changes: 2 additions & 2 deletions src/Contract/State/MainSuspendStateHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

interface MainSuspendStateHandlerInterface extends StateHandlerInterface
{
public function handle(StateMainSuspendService $stateService, StateContext $context): mixed;
public function handle(StateMainSuspendService $stateService, StateContext $context): void;

public function isResumable(Suspend $suspend, StateContext $context): bool;
public function observed(Suspend $suspend, StateContext $context): bool;
}
32 changes: 20 additions & 12 deletions src/State/Service/StateMainResumeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,37 @@

namespace Duyler\ActionBus\State\Service;

use Duyler\ActionBus\Bus\Task;
use Duyler\ActionBus\Formatter\ActionIdFormatter;
use Duyler\ActionBus\Bus\ActionContainer;
use Duyler\ActionBus\Service\ActionService;
use Duyler\ActionBus\Service\ResultService;
use Duyler\ActionBus\Service\SubscriptionService;
use Duyler\ActionBus\Service\TriggerService;
use Duyler\ActionBus\State\Service\Trait\ActionServiceTrait;
use Duyler\ActionBus\State\Service\Trait\ResultServiceTrait;
use UnitEnum;
use Duyler\ActionBus\State\Service\Trait\SubscriptionServiceTrait;
use Duyler\ActionBus\State\Service\Trait\TaskSuspendResumeServiceTrait;
use Duyler\ActionBus\State\Service\Trait\TriggerServiceTrait;
use Duyler\ActionBus\State\Suspend;

class StateMainResumeService
{
use ResultServiceTrait;
use TaskSuspendResumeServiceTrait;
use ActionServiceTrait;
use TriggerServiceTrait;
use SubscriptionServiceTrait;

public function __construct(
private readonly Task $task,
private readonly mixed $value,
private readonly Suspend $suspend,
private readonly ResultService $resultService,
private readonly ActionContainer $container,
private readonly ActionService $actionService,
private readonly TriggerService $triggerService,
private readonly SubscriptionService $subscriptionService,
) {}

public function getActionId(): string|UnitEnum
public function getActionContainer(): ActionContainer
{
return ActionIdFormatter::reverse($this->task->action->id);
}

public function getResumeValue(): mixed
{
return $this->value;
return $this->container;
}
}
17 changes: 5 additions & 12 deletions src/State/Service/StateMainSuspendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,35 @@
namespace Duyler\ActionBus\State\Service;

use Duyler\ActionBus\Bus\ActionContainer;
use Duyler\ActionBus\Bus\Task;
use Duyler\ActionBus\Formatter\ActionIdFormatter;
use Duyler\ActionBus\Service\ActionService;
use Duyler\ActionBus\Service\ResultService;
use Duyler\ActionBus\Service\SubscriptionService;
use Duyler\ActionBus\Service\TriggerService;
use Duyler\ActionBus\State\Service\Trait\ActionServiceTrait;
use Duyler\ActionBus\State\Service\Trait\ResultServiceTrait;
use Duyler\ActionBus\State\Service\Trait\SubscriptionServiceTrait;
use Duyler\ActionBus\State\Service\Trait\TaskSuspendServiceTrait;
use Duyler\ActionBus\State\Service\Trait\TaskSuspendResumeServiceTrait;
use Duyler\ActionBus\State\Service\Trait\TriggerServiceTrait;
use UnitEnum;
use Duyler\ActionBus\State\Suspend;

class StateMainSuspendService
{
use ResultServiceTrait;
use TaskSuspendServiceTrait;
use TaskSuspendResumeServiceTrait;
use ActionServiceTrait;
use TriggerServiceTrait;
use SubscriptionServiceTrait;

public function __construct(
private readonly Suspend $suspend,
private readonly ResultService $resultService,
private readonly Task $task,
private readonly ActionContainer $container,
private readonly ActionService $actionService,
private readonly TriggerService $triggerService,
private readonly SubscriptionService $subscriptionService,
) {}

public function getActionId(): string|UnitEnum
{
return ActionIdFormatter::reverse($this->task->action->id);
}

public function getContainer(): ActionContainer
public function getActionContainer(): ActionContainer
{
return $this->container;
}
Expand Down
34 changes: 34 additions & 0 deletions src/State/Service/Trait/TaskSuspendResumeServiceTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Duyler\ActionBus\State\Service\Trait;

use Duyler\ActionBus\State\Suspend;
use UnitEnum;

/**
* @property Suspend $suspend
*/
trait TaskSuspendResumeServiceTrait
{
public function getValue(): mixed
{
return $this->suspend->value;
}

public function getActionId(): string|UnitEnum
{
return $this->suspend->actionId;
}

public function setResumeValue(mixed $value): void
{
$this->suspend->setResumeValue($value);
}

public function resumeValueIsExists(): bool
{
return $this->suspend->resumeValueIsExists();
}
}
18 changes: 0 additions & 18 deletions src/State/Service/Trait/TaskSuspendServiceTrait.php

This file was deleted.

41 changes: 22 additions & 19 deletions src/State/StateMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,53 +92,56 @@ public function suspend(Task $task): void
{
$handlers = $this->stateHandlerStorage->getMainSuspend();

$value = $task->getValue();

if (null === $value) {
$this->context->addResumeValue($task->action->id, $value);
return;
}
$suspend = new Suspend(ActionIdFormatter::reverse($task->action->id), $task->getValue());

$stateService = new StateMainSuspendService(
$suspend,
$this->resultService,
$task,
$this->actionContainerCollection->get($task->action->id),
$this->actionService,
$this->triggerService,
$this->subscriptionService,
);

$this->context->addSuspend($task->action->id, $suspend);

foreach ($handlers as $handler) {
$context = $this->contextScope->getContext($handler::class);
$suspend = new Suspend(ActionIdFormatter::reverse($task->action->id), $stateService->getValue());
if ($handler->isResumable($suspend, $context)) {
$resumeValue = $handler->handle($stateService, $context);
$this->context->addResumeValue($task->action->id, $resumeValue);
return;
if ($handler->observed($suspend, $context)) {
$handler->handle($stateService, $context);
}
}

$result = is_callable($value) ? $value() : $value;
$this->context->addResumeValue($task->action->id, $result);
}

#[Override]
public function resume(Task $task): void
{
$handlers = $this->stateHandlerStorage->getMainResume();

$resumeValue = $this->context->getResumeValue($task->action->id);
$suspend = $this->context->getSuspend($task->action->id);

$stateService = new StateMainResumeService(
$task,
$resumeValue,
$suspend,
$this->resultService,
$this->actionContainerCollection->get($task->action->id),
$this->actionService,
$this->triggerService,
$this->subscriptionService,
);

foreach ($handlers as $handler) {
$handler->handle($stateService, $this->contextScope->getContext($handler::class));
$context = $this->contextScope->getContext($handler::class);
if ($handler->observed($suspend, $context)) {
$handler->handle($stateService, $context);
}
}

if ($suspend->resumeValueIsExists()) {
$task->resume($suspend->getResumeValue());
return;
}

$resumeValue = is_callable($suspend->value) ? ($suspend->value)() : $suspend->value;
$task->resume($resumeValue);
}

Expand Down
12 changes: 6 additions & 6 deletions src/State/StateSuspendContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

class StateSuspendContext
{
/** @var array<string, mixed[]> */
private array $resumeValues = [];
/** @var array<string, Suspend[]> */
private array $suspend = [];

public function addResumeValue(string $actionId, mixed $value): void
public function addSuspend(string $actionId, Suspend $suspend): void
{
$this->resumeValues[$actionId][] = $value;
$this->suspend[$actionId][] = $suspend;
}

public function getResumeValue(string $actionId): mixed
public function getSuspend(string $actionId): Suspend
{
return array_shift($this->resumeValues[$actionId]);
return array_shift($this->suspend[$actionId]);
}
}
23 changes: 20 additions & 3 deletions src/State/Suspend.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@

use UnitEnum;

readonly class Suspend
class Suspend
{
private mixed $resumeValue = null;

public function __construct(
public string|UnitEnum $actionId,
public mixed $value,
public readonly string|UnitEnum $actionId,
public readonly mixed $value,
) {}

public function setResumeValue(mixed $value): void
{
$this->resumeValue = $value;
}

public function getResumeValue(): mixed
{
return $this->resumeValue;
}

public function resumeValueIsExists(): bool
{
return null !== $this->resumeValue;
}
}
18 changes: 12 additions & 6 deletions tests/Functional/State/MainSuspendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,22 @@ public function suspend_with_callback()
class MainSuspendStateHandler implements MainSuspendStateHandlerInterface
{
#[Override]
public function handle(StateMainSuspendService $stateService, StateContext $context): mixed
public function handle(StateMainSuspendService $stateService, StateContext $context): void
{
if ($stateService->getActionId() === 'TestSuspend1') {
$stateService->getContainer();
$stateService->getActionContainer();
}

/** @var callable $value */
$value = $stateService->getValue();

$result = $value();

return fn() => $result . ', World!';
$stateService->setResumeValue(fn() => $result . ', World!');
}

#[Override]
public function isResumable(Suspend $suspend, StateContext $context): bool
public function observed(Suspend $suspend, StateContext $context): bool
{
return true;
}
Expand All @@ -130,12 +130,18 @@ public function isResumable(Suspend $suspend, StateContext $context): bool
class MainResumeStateHandler implements MainResumeStateHandlerInterface
{
#[Override]
public function handle(StateMainResumeService $stateService, StateContext $context): mixed
public function handle(StateMainResumeService $stateService, StateContext $context): void
{
$stateService->getActionId();
if ($stateService->resultIsExists('TestSuspend2')) {
$stateService->getResult('TestSuspend2');
}
return $stateService->getResumeValue();
$stateService->resumeValueIsExists();
$stateService->getActionContainer();
}

public function observed(Suspend $suspend, StateContext $context): bool
{
return true;
}
}
5 changes: 3 additions & 2 deletions tests/Unit/State/StateContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Duyler\ActionBus\Test\Unit\State;

use Duyler\ActionBus\State\StateSuspendContext;
use Duyler\ActionBus\State\Suspend;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

Expand All @@ -15,8 +16,8 @@ class StateContextTest extends TestCase
#[Test]
public function addResumeValue_with_string(): void
{
$this->stateContext->addResumeValue('actionId', 'value');
$this->assertSame('value', $this->stateContext->getResumeValue('actionId'));
$this->stateContext->addSuspend('actionId', new Suspend('actionId', 'value'));
$this->assertSame('value', $this->stateContext->getSuspend('actionId')->value);
}

protected function setUp(): void
Expand Down

0 comments on commit a160307

Please sign in to comment.