diff --git a/.gitignore b/.gitignore index a7f372d..e26945a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ phpstan.neon testbench.yaml vendor node_modules +.DS_Store diff --git a/composer.json b/composer.json index ec66172..1f961d5 100644 --- a/composer.json +++ b/composer.json @@ -52,7 +52,7 @@ "profile": "vendor/bin/pest --profile --colors=always --order-by=random --configuration=phpunit.xml.dist", "coverage": "vendor/bin/pest --coverage --colors=always --order-by=random --configuration=phpunit.xml.dist", "coveragep": "vendor/bin/pest --parallel --coverage --colors=always --order-by=random --configuration=phpunit.xml.dist", - "pint": "vendor/bin/pint", + "pint": "vendor/bin/pint --config=pint.json", "lint": "@pint", "lintc": "vendor/bin/pint && (git diff-index --quiet HEAD || (git add . && git commit -m 'chore: Fix styling'))", "infection": "vendor/bin/infection --test-framework=pest --show-mutations --threads=max --min-msi=100 --min-covered-msi=100 --ansi" diff --git a/src/Actor/Machine.php b/src/Actor/Machine.php index e146e6d..a05a32b 100644 --- a/src/Actor/Machine.php +++ b/src/Actor/Machine.php @@ -493,7 +493,7 @@ public function result(): mixed $resultBehavior = new $resultBehavior(); } - /* @var \Tarfinlabs\EventMachine\Behavior\ResultBehavior $resultBehavior */ + /* @var callable $resultBehavior */ return $resultBehavior( $this->state->context, $this->state->currentEventBehavior, diff --git a/src/Actor/State.php b/src/Actor/State.php index 11498ac..b0bde69 100644 --- a/src/Actor/State.php +++ b/src/Actor/State.php @@ -8,6 +8,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; use Tarfinlabs\EventMachine\ContextManager; +use Tarfinlabs\EventMachine\EventCollection; use Tarfinlabs\EventMachine\Enums\SourceType; use Tarfinlabs\EventMachine\Enums\InternalEvent; use Tarfinlabs\EventMachine\Models\MachineEvent; @@ -41,9 +42,9 @@ public function __construct( public ContextManager $context, public ?StateDefinition $currentStateDefinition, public ?EventBehavior $currentEventBehavior = null, - public ?Collection $history = null, + public ?EventCollection $history = null, ) { - $this->history ??= (new MachineEvent())->newCollection(); + $this->history ??= new EventCollection(); $this->updateMachineValueFromState(); } diff --git a/src/Behavior/ActionBehavior.php b/src/Behavior/ActionBehavior.php index da139fa..086181f 100644 --- a/src/Behavior/ActionBehavior.php +++ b/src/Behavior/ActionBehavior.php @@ -4,8 +4,6 @@ namespace Tarfinlabs\EventMachine\Behavior; -use Tarfinlabs\EventMachine\ContextManager; - /** * ActionBehavior class. * @@ -14,16 +12,4 @@ */ abstract class ActionBehavior extends InvokableBehavior { - /** - * Invokes the method with the given parameters. - * - * @param ContextManager $context Provides access to the context in which the method is being invoked. - * @param EventBehavior $eventBehavior The event behavior associated with the method invocation. - * @param array|null $arguments Optional parameters to be passed to the method. - */ - abstract public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null, - ): void; } diff --git a/src/Behavior/GuardBehavior.php b/src/Behavior/GuardBehavior.php index b6515a0..015f5f6 100644 --- a/src/Behavior/GuardBehavior.php +++ b/src/Behavior/GuardBehavior.php @@ -4,8 +4,6 @@ namespace Tarfinlabs\EventMachine\Behavior; -use Tarfinlabs\EventMachine\ContextManager; - /** * Class GuardBehavior. * @@ -14,18 +12,4 @@ */ abstract class GuardBehavior extends InvokableBehavior { - /** - * Invokes the method. - * - * @param ContextManager $context The context manager. - * @param EventBehavior $eventBehavior The event behavior. - * @param array|null $arguments The optional arguments for the method. - * - * @return bool Returns true if the method is invoked successfully, false otherwise. - */ - abstract public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null, - ): bool; } diff --git a/src/Behavior/InvokableBehavior.php b/src/Behavior/InvokableBehavior.php index 915f478..3f165ef 100644 --- a/src/Behavior/InvokableBehavior.php +++ b/src/Behavior/InvokableBehavior.php @@ -4,8 +4,12 @@ namespace Tarfinlabs\EventMachine\Behavior; +use ReflectionMethod; +use ReflectionFunction; +use ReflectionUnionType; use Illuminate\Support\Str; use Illuminate\Support\Collection; +use Tarfinlabs\EventMachine\Actor\State; use Tarfinlabs\EventMachine\ContextManager; use Tarfinlabs\EventMachine\Exceptions\MissingMachineContextException; @@ -30,33 +34,13 @@ abstract class InvokableBehavior * * @return void */ - public function __construct( - protected ?Collection $eventQueue = null - ) { + public function __construct(protected ?Collection $eventQueue = null) + { if ($this->eventQueue === null) { $this->eventQueue = new Collection(); } } - /** - * Executes the behavior with the given context and event. - * - * This method defines the contract for implementing behaviors - * within classes. The behavior should be directly invokable by - * passing in a ContextManager instance and an array of event payload. - * - * @param ContextManager $context The context to be used during - * invocation. - * @param \Tarfinlabs\EventMachine\Behavior\EventBehavior $eventBehavior The event related to the - * current behavior. - * @param array|null $arguments The arguments to be passed to the behavior. - */ - abstract public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null, - ); - /** * Raises an event by adding it to the event queue. * @@ -133,4 +117,54 @@ public static function getType(): string ->classBasename() ->toString(); } + + /** + * Injects invokable behavior parameters. + * + * Retrieves the parameters of the given invokable behavior and injects the corresponding values + * based on the provided state, event behavior, and action arguments. + * The injected values are added to an array and returned. + * + * @param callable $actionBehavior The invokable behavior to inject parameters for. + * @param State $state The state object used for parameter matching. + * @param EventBehavior|null $eventBehavior The event behavior used for parameter matching. (Optional) + * @param array|null $actionArguments The action arguments used for parameter matching. (Optional) + * + * @return array The injected invokable behavior parameters. + * + * @throws \ReflectionException + */ + public static function injectInvokableBehaviorParameters( + callable $actionBehavior, + State $state, + ?EventBehavior $eventBehavior = null, + ?array $actionArguments = null, + ): array { + $invocableBehaviorParameters = []; + + $invocableBehaviorReflection = $actionBehavior instanceof self + ? new ReflectionMethod($actionBehavior, '__invoke') + : new ReflectionFunction($actionBehavior); + + foreach ($invocableBehaviorReflection->getParameters() as $parameter) { + $parameterType = $parameter->getType(); + + $typeName = $parameterType instanceof ReflectionUnionType + ? $parameterType->getTypes()[0]->getName() + : $parameterType->getName(); + + $value = match (true) { + is_a($state->context, $typeName) => $state->context, // ContextManager + is_a($eventBehavior, $typeName) => $eventBehavior, // EventBehavior + is_a($state, $typeName) => $state, // State + is_a($state->history, $typeName) => $state->history, // EventCollection + $typeName === 'array' => $actionArguments, // Behavior Arguments + default => null, + }; + + $invocableBehaviorParameters[] = $value; + } + + return $invocableBehaviorParameters; + } } diff --git a/src/Behavior/ResultBehavior.php b/src/Behavior/ResultBehavior.php index 0886773..2551a83 100644 --- a/src/Behavior/ResultBehavior.php +++ b/src/Behavior/ResultBehavior.php @@ -4,13 +4,6 @@ namespace Tarfinlabs\EventMachine\Behavior; -use Tarfinlabs\EventMachine\ContextManager; - abstract class ResultBehavior extends InvokableBehavior { - abstract public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null, - ): mixed; } diff --git a/src/Behavior/ValidationGuardBehavior.php b/src/Behavior/ValidationGuardBehavior.php index ad2046c..64cabe6 100644 --- a/src/Behavior/ValidationGuardBehavior.php +++ b/src/Behavior/ValidationGuardBehavior.php @@ -4,8 +4,6 @@ namespace Tarfinlabs\EventMachine\Behavior; -use Tarfinlabs\EventMachine\ContextManager; - /** * An abstract class that represents a validation guard behavior. * @@ -18,19 +16,4 @@ abstract class ValidationGuardBehavior extends GuardBehavior { /** @var string|null Holds an error message, which is initially null. */ public ?string $errorMessage = null; - - /** - * Invokes the method. - * - * @param ContextManager $context The context manager. - * @param EventBehavior $eventBehavior The event behavior. - * @param array|null $arguments The arguments for the method (optional). - * - * @return bool Returns true if the method invocation was successful, false otherwise. - */ - abstract public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null, - ): bool; } diff --git a/src/Definition/MachineDefinition.php b/src/Definition/MachineDefinition.php index e0586ff..2cd3ec7 100644 --- a/src/Definition/MachineDefinition.php +++ b/src/Definition/MachineDefinition.php @@ -684,6 +684,8 @@ public function transition( * * @param string $actionDefinition The action definition, either a class * @param EventBehavior|null $eventBehavior The event (optional). + * + * @throws \ReflectionException */ public function runAction( string $actionDefinition, @@ -720,8 +722,16 @@ public function runAction( // Get the number of events in the queue before the action is executed. $numberOfEventsInQueue = $this->eventQueue->count(); - // Execute the action behavior. - $actionBehavior($state->context, $eventBehavior, $actionArguments); + // Inject action behavior parameters + $actionBehaviorParemeters = InvokableBehavior::injectInvokableBehaviorParameters( + actionBehavior: $actionBehavior, + state: $state, + eventBehavior: $eventBehavior, + actionArguments: $actionArguments + ); + + // Execute the action behavior + ($actionBehavior)(...$actionBehaviorParemeters); // Get the number of events in the queue after the action is executed. $newNumberOfEventsInQueue = $this->eventQueue->count(); diff --git a/src/Definition/TransitionBranch.php b/src/Definition/TransitionBranch.php index 835197b..3c0a743 100644 --- a/src/Definition/TransitionBranch.php +++ b/src/Definition/TransitionBranch.php @@ -166,6 +166,8 @@ protected function initializeInlineBehaviors(array $inlineBehaviors, BehaviorTyp * If there are no actions associated with the transition definition, do nothing. * * @param EventBehavior|null $eventBehavior The event or null if none is provided. + * + * @throws \ReflectionException */ public function runActions( State $state, diff --git a/src/Definition/TransitionDefinition.php b/src/Definition/TransitionDefinition.php index 3de8d27..1c8fb6c 100644 --- a/src/Definition/TransitionDefinition.php +++ b/src/Definition/TransitionDefinition.php @@ -10,6 +10,7 @@ use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\GuardBehavior; use Tarfinlabs\EventMachine\Enums\TransitionProperty; +use Tarfinlabs\EventMachine\Behavior\InvokableBehavior; use Tarfinlabs\EventMachine\Behavior\ValidationGuardBehavior; /** @@ -135,6 +136,8 @@ protected function isAMultiPathGuardedTransition(null|array|string $transitionCo * * @return TransitionDefinition|null The first eligible transition or * null if no eligible transition is found. + * + * @throws \ReflectionException */ public function getFirstValidTransitionBranch( EventBehavior $eventBehavior, @@ -162,7 +165,18 @@ public function getFirstValidTransitionBranch( $guardBehavior->validateRequiredContext($state->context); } - if ($guardBehavior($state->context, $eventBehavior, $guardArguments) === false) { + // Inject guard behavior parameters + $guardBehaviorParameters = InvokableBehavior::injectInvokableBehaviorParameters( + actionBehavior: $guardBehavior, + state: $state, + eventBehavior: $eventBehavior, + actionArguments: $guardArguments, + ); + + // Execute the guard behavior + $guardResult = ($guardBehavior)(...$guardBehaviorParameters); + + if ($guardResult === false) { $guardsPassed = false; $payload = null; diff --git a/src/EventCollection.php b/src/EventCollection.php new file mode 100644 index 0000000..23d178e --- /dev/null +++ b/src/EventCollection.php @@ -0,0 +1,11 @@ + [ - 'additionAction' => function (ContextManager $context, EventDefinition $eventDefinition): void { - $context->set('count', $context->get('count') + $eventDefinition->payload['value']); + 'additionAction' => function (ContextManager $context, EventBehavior $eventBehavior): void { + $context->set('count', $context->get('count') + $eventBehavior->payload['value']); }, - 'subtractionAction' => function (ContextManager $context, EventDefinition $eventDefinition): void { - $context->set('count', $context->get('count') - $eventDefinition->payload['value']); + 'subtractionAction' => function (ContextManager $context, EventBehavior $eventBehavior): void { + $context->set('count', $context->get('count') - $eventBehavior->payload['value']); }, 'incrementAction' => function (ContextManager $context): void { $context->set('count', $context->get('count') + 1); @@ -92,19 +92,15 @@ $value = random_int(10, 20); $multipleWithItselfAction = new class extends ResultBehavior { - public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): int { - return $eventBehavior->payload['value'] * $eventBehavior->payload['value']; + public function __invoke(EventBehavior $event): int + { + return $event->payload['value'] * $event->payload['value']; } }; // 2. Act $result = $multipleWithItselfAction( - context: new ContextManager(), - eventBehavior: EventDefinition::from([ + EventDefinition::from([ 'type' => 'ADD', 'payload' => [ 'value' => $value, diff --git a/tests/BehaviorDependencyInjectionTest.php b/tests/BehaviorDependencyInjectionTest.php new file mode 100644 index 0000000..6874af9 --- /dev/null +++ b/tests/BehaviorDependencyInjectionTest.php @@ -0,0 +1,52 @@ + [ + 'value' => 1, + ], + 'initial' => 'ready', + 'states' => [ + 'ready' => [ + 'on' => [ + 'CTX' => ['actions' => 'contextAction'], + ], + ], + ], + ], + behavior: [ + 'actions' => [ + 'contextAction' => function ( + ContextManager $c, + EventBehavior $e, + State $s, + EventCollection $ec + ): void { + expect($c)->toBeInstanceOf(ContextManager::class); + expect($c->value)->toBe(1); + + expect($e)->toBeInstanceOf(EventBehavior::class); + expect($e->type)->toBe('CTX'); + + expect($s)->toBeInstanceOf(State::class); + expect($s->value)->toBe(['machine.ready']); + + expect($ec)->toBeInstanceOf(EventCollection::class); + expect($ec->count())->toBe(7); + }, + ], + ], + )); + + $machine->send(['type' => 'CTX']); +}); diff --git a/tests/EventStoreTest.php b/tests/EventStoreTest.php index 903640e..b4b1860 100644 --- a/tests/EventStoreTest.php +++ b/tests/EventStoreTest.php @@ -4,7 +4,7 @@ use Tarfinlabs\EventMachine\Actor\Machine; use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Definition\EventDefinition; +use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Definition\MachineDefinition; it('stores external events', function (): void { @@ -81,8 +81,8 @@ ], behavior: [ 'actions' => [ - 'additionAction' => function (ContextManager $context, EventDefinition $eventDefinition): void { - $context->set('count', $context->get('count') + $eventDefinition->payload['value']); + 'additionAction' => function (ContextManager $context, EventBehavior $eventBehavior): void { + $context->set('count', $context->get('count') + $eventBehavior->payload['value']); }, ], ] diff --git a/tests/InvokableBehaviorArgumentsTest.php b/tests/InvokableBehaviorArgumentsTest.php index 963dfad..e48af7b 100644 --- a/tests/InvokableBehaviorArgumentsTest.php +++ b/tests/InvokableBehaviorArgumentsTest.php @@ -25,8 +25,8 @@ ], behavior: [ 'actions' => [ - 'additionAction' => function (ContextManager $context, EventDefinition $eventDefinition, ?array $arguments = null): void { - $context->count += array_sum($arguments); + 'additionAction' => function (ContextManager $ctx, EventDefinition $ed, ?array $arguments = null): void { + $ctx->count += array_sum($arguments); }, ], ], diff --git a/tests/Stubs/Actions/IsOddAction.php b/tests/Stubs/Actions/IsOddAction.php index c02bc80..01f0cb9 100644 --- a/tests/Stubs/Actions/IsOddAction.php +++ b/tests/Stubs/Actions/IsOddAction.php @@ -5,7 +5,6 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Actions; use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; class IsOddAction extends ActionBehavior @@ -14,7 +13,7 @@ class IsOddAction extends ActionBehavior 'counts.oddCount' => 'integer', ]; - public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void + public function __invoke(ContextManager $context): void { $context->set('counts.oddCount', 1); } diff --git a/tests/Stubs/Guards/IsOddGuard.php b/tests/Stubs/Guards/IsOddGuard.php index ab65705..2e02dcf 100644 --- a/tests/Stubs/Guards/IsOddGuard.php +++ b/tests/Stubs/Guards/IsOddGuard.php @@ -4,8 +4,6 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Guards; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\GuardBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; @@ -15,11 +13,8 @@ class IsOddGuard extends GuardBehavior 'counts.oddCount' => 'integer', ]; - public function __invoke( - TrafficLightsContext|ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): bool { + public function __invoke(TrafficLightsContext $context): bool + { return $context->count % 2 === 1; } } diff --git a/tests/Stubs/Guards/IsTimerValidValidationGuard.php b/tests/Stubs/Guards/IsTimerValidValidationGuard.php index f392cc1..5cc3972 100644 --- a/tests/Stubs/Guards/IsTimerValidValidationGuard.php +++ b/tests/Stubs/Guards/IsTimerValidValidationGuard.php @@ -13,7 +13,7 @@ class IsTimerValidValidationGuard extends ValidationGuardBehavior public function __invoke( ContextManager $context, EventBehavior $eventBehavior, - ?array $arguments = null + ?array $arguments = null, ): bool { $value = $eventBehavior->payload['value']; $result = $value > (int) $arguments[0]; diff --git a/tests/Stubs/Guards/IsValidatedOddGuard.php b/tests/Stubs/Guards/IsValidatedOddGuard.php index 837a49d..6195657 100644 --- a/tests/Stubs/Guards/IsValidatedOddGuard.php +++ b/tests/Stubs/Guards/IsValidatedOddGuard.php @@ -4,8 +4,6 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Guards; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ValidationGuardBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; @@ -16,11 +14,8 @@ class IsValidatedOddGuard extends ValidationGuardBehavior ]; public ?string $errorMessage = 'Count is not odd'; - public function __invoke( - TrafficLightsContext|ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): bool { + public function __invoke(TrafficLightsContext $context): bool + { return $context->count % 2 === 1; } } diff --git a/tests/Stubs/Machines/Asd/Actions/AAction.php b/tests/Stubs/Machines/Asd/Actions/AAction.php index ae8f56d..fa886c5 100644 --- a/tests/Stubs/Machines/Asd/Actions/AAction.php +++ b/tests/Stubs/Machines/Asd/Actions/AAction.php @@ -4,14 +4,12 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Asd\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Models\ModelA; class AAction extends ActionBehavior { - public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void + public function __invoke(): void { ModelA::create([ 'value' => 'lorem ipsum dolor', diff --git a/tests/Stubs/Machines/Asd/Actions/DAction.php b/tests/Stubs/Machines/Asd/Actions/DAction.php index c85e94d..c317a78 100644 --- a/tests/Stubs/Machines/Asd/Actions/DAction.php +++ b/tests/Stubs/Machines/Asd/Actions/DAction.php @@ -4,17 +4,13 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Asd\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; +use RuntimeException; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; class DAction extends ActionBehavior { - /** - * @throws \Exception - */ - public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void + public function __invoke(): void { - throw new \Exception('error'); + throw new RuntimeException('error'); } } diff --git a/tests/Stubs/Machines/Asd/Actions/SAction.php b/tests/Stubs/Machines/Asd/Actions/SAction.php index 434c9cb..31f01b7 100644 --- a/tests/Stubs/Machines/Asd/Actions/SAction.php +++ b/tests/Stubs/Machines/Asd/Actions/SAction.php @@ -4,17 +4,12 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Asd\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Models\ModelA; class SAction extends ActionBehavior { - /** - * @throws \Exception - */ - public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void + public function __invoke(): void { ModelA::first()->update([ 'value' => 'new value', diff --git a/tests/Stubs/Machines/Asd/Actions/SleepAction.php b/tests/Stubs/Machines/Asd/Actions/SleepAction.php index 8ac96b3..76b683c 100644 --- a/tests/Stubs/Machines/Asd/Actions/SleepAction.php +++ b/tests/Stubs/Machines/Asd/Actions/SleepAction.php @@ -4,16 +4,11 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Asd\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; class SleepAction extends ActionBehavior { - /** - * @throws \Exception - */ - public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void + public function __invoke(): void { sleep(1); } diff --git a/tests/Stubs/Machines/CalculatorMachine.php b/tests/Stubs/Machines/CalculatorMachine.php new file mode 100644 index 0000000..b9c5af1 --- /dev/null +++ b/tests/Stubs/Machines/CalculatorMachine.php @@ -0,0 +1,51 @@ + 'ready', + 'context' => [ + 'result' => 0, + ], + 'states' => [ + 'ready' => [ + 'on' => [ + 'ADD' => ['actions' => 'additionAction'], + 'SUB' => ['actions' => 'subtractionAction'], + 'MUL' => ['actions' => 'multiplicationAction'], + 'DIV' => ['actions' => 'divisionAction'], + ], + ], + ], + ], + behavior: [ + 'actions' => [ + 'additionAction' => function (ContextManager $c, EventDefinition $e): void { + $c->result = $c->result + $e->payload['value']; + }, + 'subtractionAction' => function (ContextManager $ctx, EventDefinition $evt): void { + $ctx->result = $ctx->result - $evt->payload['value']; + }, + 'multiplicationAction' => function (ContextManager $contextManager, EventDefinition $eventDefinition): void { + $contextManager->result = $contextManager->result * $eventDefinition->payload['value']; + }, + 'divisionAction' => function (ContextManager $manager, EventDefinition $event): void { + $manager->result = $manager->result / $event->payload['value']; + }, + ], + ], + ); + } +} diff --git a/tests/Stubs/Machines/Qwerty/Actions/TAction.php b/tests/Stubs/Machines/Qwerty/Actions/TAction.php index 9bc93f2..816c44e 100644 --- a/tests/Stubs/Machines/Qwerty/Actions/TAction.php +++ b/tests/Stubs/Machines/Qwerty/Actions/TAction.php @@ -11,8 +11,10 @@ class TAction extends ActionBehavior { - public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void - { + public function __invoke( + ContextManager $context, + EventBehavior $eventBehavior + ): void { $this->raise(new TEvent(actor: $eventBehavior->actor($context))); } } diff --git a/tests/Stubs/Machines/TrafficLights/Actions/AddAnotherValueAction.php b/tests/Stubs/Machines/TrafficLights/Actions/AddAnotherValueAction.php index 3e90db6..424e86b 100644 --- a/tests/Stubs/Machines/TrafficLights/Actions/AddAnotherValueAction.php +++ b/tests/Stubs/Machines/TrafficLights/Actions/AddAnotherValueAction.php @@ -4,16 +4,16 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Events\AddAnotherValueEvent; class AddAnotherValueAction extends ActionBehavior { - public function __invoke(ContextManager|TrafficLightsContext $context, EventBehavior|AddAnotherValueEvent $eventBehavior, ?array $arguments = null): void - { + public function __invoke( + TrafficLightsContext $context, + AddAnotherValueEvent $eventBehavior + ): void { $context->count += $eventBehavior->value; } } diff --git a/tests/Stubs/Machines/TrafficLights/Actions/AddValueAction.php b/tests/Stubs/Machines/TrafficLights/Actions/AddValueAction.php index 58adbb0..884ebc1 100644 --- a/tests/Stubs/Machines/TrafficLights/Actions/AddValueAction.php +++ b/tests/Stubs/Machines/TrafficLights/Actions/AddValueAction.php @@ -4,16 +4,16 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Actions; -use Tarfinlabs\EventMachine\ContextManager; use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; class AddValueAction extends ActionBehavior { - public function __invoke(ContextManager|TrafficLightsContext $context, EventBehavior $eventBehavior, ?array $arguments = null): void - { - /* @var \Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext $context */ + public function __invoke( + TrafficLightsContext $context, + EventBehavior $eventBehavior + ): void { $context->count += $eventBehavior->payload['value']; } } diff --git a/tests/Stubs/Machines/TrafficLights/Actions/DecrementAction.php b/tests/Stubs/Machines/TrafficLights/Actions/DecrementAction.php index a7cd9fb..bb695a6 100644 --- a/tests/Stubs/Machines/TrafficLights/Actions/DecrementAction.php +++ b/tests/Stubs/Machines/TrafficLights/Actions/DecrementAction.php @@ -4,19 +4,13 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; class DecrementAction extends ActionBehavior { - public function __invoke( - ContextManager|TrafficLightsContext $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): void { - /* @var \Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext $context */ + public function __invoke(TrafficLightsContext $context): void + { $context->count--; } } diff --git a/tests/Stubs/Machines/TrafficLights/Actions/DoNothingInsideClassAction.php b/tests/Stubs/Machines/TrafficLights/Actions/DoNothingInsideClassAction.php index f0f0a1d..bc2efed 100644 --- a/tests/Stubs/Machines/TrafficLights/Actions/DoNothingInsideClassAction.php +++ b/tests/Stubs/Machines/TrafficLights/Actions/DoNothingInsideClassAction.php @@ -4,14 +4,11 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; class DoNothingInsideClassAction extends ActionBehavior { - public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void + public function __invoke(): void { - // Do nothing. } } diff --git a/tests/Stubs/Machines/TrafficLights/Actions/IncrementAction.php b/tests/Stubs/Machines/TrafficLights/Actions/IncrementAction.php index 4b245bb..3307b33 100644 --- a/tests/Stubs/Machines/TrafficLights/Actions/IncrementAction.php +++ b/tests/Stubs/Machines/TrafficLights/Actions/IncrementAction.php @@ -4,16 +4,13 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; class IncrementAction extends ActionBehavior { - public function __invoke(ContextManager|TrafficLightsContext $context, EventBehavior $eventBehavior, ?array $arguments = null): void + public function __invoke(TrafficLightsContext $context): void { - /* @var \Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext $context */ $context->count++; } } diff --git a/tests/Stubs/Machines/TrafficLights/Actions/MultiplyByTwoAction.php b/tests/Stubs/Machines/TrafficLights/Actions/MultiplyByTwoAction.php index 4c57768..cb69a1d 100644 --- a/tests/Stubs/Machines/TrafficLights/Actions/MultiplyByTwoAction.php +++ b/tests/Stubs/Machines/TrafficLights/Actions/MultiplyByTwoAction.php @@ -4,8 +4,6 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Actions; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; @@ -13,9 +11,8 @@ class MultiplyByTwoAction extends ActionBehavior { public bool $shouldLog = true; - public function __invoke(ContextManager|TrafficLightsContext $context, EventBehavior $eventBehavior, ?array $arguments = null): void + public function __invoke(TrafficLightsContext $context): void { - /* @var \Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext $context */ $context->count *= 2; } } diff --git a/tests/Stubs/Machines/TrafficLights/Actions/SubtractValueAction.php b/tests/Stubs/Machines/TrafficLights/Actions/SubtractValueAction.php index 1d6bf97..8429fcf 100644 --- a/tests/Stubs/Machines/TrafficLights/Actions/SubtractValueAction.php +++ b/tests/Stubs/Machines/TrafficLights/Actions/SubtractValueAction.php @@ -4,16 +4,16 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Actions; -use Tarfinlabs\EventMachine\ContextManager; use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; class SubtractValueAction extends ActionBehavior { - public function __invoke(ContextManager|TrafficLightsContext $context, EventBehavior $eventBehavior, ?array $arguments = null): void - { - /* @var \Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext $context */ + public function __invoke( + TrafficLightsContext $context, + EventBehavior $eventBehavior, + ): void { $context->count -= $eventBehavior->payload['value']; } } diff --git a/tests/Stubs/Machines/TrafficLights/Guards/IsEvenGuard.php b/tests/Stubs/Machines/TrafficLights/Guards/IsEvenGuard.php index bb20cb7..df76d18 100644 --- a/tests/Stubs/Machines/TrafficLights/Guards/IsEvenGuard.php +++ b/tests/Stubs/Machines/TrafficLights/Guards/IsEvenGuard.php @@ -4,8 +4,6 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\Guards; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ValidationGuardBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\TrafficLights\TrafficLightsContext; @@ -14,11 +12,8 @@ class IsEvenGuard extends ValidationGuardBehavior public ?string $errorMessage = 'Count is not even'; public bool $shouldLog = true; - public function __invoke( - ContextManager|TrafficLightsContext $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): bool { + public function __invoke(TrafficLightsContext $context): bool + { return $context->count % 2 === 0; } } diff --git a/tests/Stubs/Machines/Xyz/Actions/XAction.php b/tests/Stubs/Machines/Xyz/Actions/XAction.php index ee36037..abb839b 100644 --- a/tests/Stubs/Machines/Xyz/Actions/XAction.php +++ b/tests/Stubs/Machines/Xyz/Actions/XAction.php @@ -5,16 +5,12 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Xyz\Actions; use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; class XAction extends ActionBehavior { - public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): void { + public function __invoke(ContextManager $context): void + { $context->value .= 'x'; $this->raise(['type' => '@x']); diff --git a/tests/Stubs/Machines/Xyz/Actions/YAction.php b/tests/Stubs/Machines/Xyz/Actions/YAction.php index 6887f84..2dec6e9 100644 --- a/tests/Stubs/Machines/Xyz/Actions/YAction.php +++ b/tests/Stubs/Machines/Xyz/Actions/YAction.php @@ -5,17 +5,13 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Xyz\Actions; use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; use Tarfinlabs\EventMachine\Tests\Stubs\Machines\Xyz\Events\YEvent; class YAction extends ActionBehavior { - public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): void { + public function __invoke(ContextManager $context): void + { $context->value .= 'y'; $this->raise(new YEvent()); diff --git a/tests/Stubs/Machines/Xyz/Actions/ZAction.php b/tests/Stubs/Machines/Xyz/Actions/ZAction.php index 09cb7d7..67f8555 100644 --- a/tests/Stubs/Machines/Xyz/Actions/ZAction.php +++ b/tests/Stubs/Machines/Xyz/Actions/ZAction.php @@ -5,16 +5,12 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Machines\Xyz\Actions; use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ActionBehavior; class ZAction extends ActionBehavior { - public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): void { + public function __invoke(ContextManager $context): void + { $context->value .= 'z'; $this->raise(['type' => '@z']); diff --git a/tests/Stubs/Results/GreenResult.php b/tests/Stubs/Results/GreenResult.php index c2b7262..8df37fa 100644 --- a/tests/Stubs/Results/GreenResult.php +++ b/tests/Stubs/Results/GreenResult.php @@ -5,17 +5,12 @@ namespace Tarfinlabs\EventMachine\Tests\Stubs\Results; use Illuminate\Support\Carbon; -use Tarfinlabs\EventMachine\ContextManager; -use Tarfinlabs\EventMachine\Behavior\EventBehavior; use Tarfinlabs\EventMachine\Behavior\ResultBehavior; class GreenResult extends ResultBehavior { - public function __invoke( - ContextManager $context, - EventBehavior $eventBehavior, - ?array $arguments = null - ): Carbon { + public function __invoke(): Carbon + { return now(); } }