Skip to content

Commit

Permalink
Merge pull request #71 from tarfin-labs/WEB-4311-event-machine-guardl…
Browse files Browse the repository at this point in the history
…arin-start-eventlerini-artik-kaydetmeyelim

Web 4311 event machine guardlarin start eventlerini artik kaydetmeyelim
  • Loading branch information
deligoez authored Dec 13, 2023
2 parents 5b994c1 + c06092f commit 1ce5b93
Show file tree
Hide file tree
Showing 40 changed files with 55 additions and 68 deletions.
6 changes: 3 additions & 3 deletions src/Actor/Machine.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public static function definition(): ?MachineDefinition
* @return self The newly created and initialized machine instance.
*/
public static function create(
MachineDefinition|array $definition = null,
State|string $state = null,
MachineDefinition|array|null $definition = null,
State|string|null $state = null,
): self {
if (is_array($definition)) {
$definition = MachineDefinition::define(
Expand All @@ -135,7 +135,7 @@ public static function create(
*
* @return self The started machine instance.
*/
public function start(State|string $state = null): self
public function start(State|string|null $state = null): self
{
$this->state = match (true) {
$state === null => $this->definition->getInitialState(),
Expand Down
4 changes: 2 additions & 2 deletions src/Actor/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public function setCurrentStateDefinition(StateDefinition $stateDefinition): sel
*/
public function setInternalEventBehavior(
InternalEvent $type,
string $placeholder = null,
array $payload = null,
?string $placeholder = null,
?array $payload = null,
bool $shouldLog = false,
): self {
$eventDefinition = new EventDefinition(
Expand Down
2 changes: 1 addition & 1 deletion src/Behavior/ActionBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ abstract class ActionBehavior extends InvokableBehavior
abstract public function __invoke(
ContextManager $context,
EventBehavior $eventBehavior,
array $arguments = null,
?array $arguments = null,
): void;
}
2 changes: 1 addition & 1 deletion src/Behavior/EventBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
public null|string|Optional $type = null,
public null|array|Optional $payload = null,
#[WithoutValidation]

Check failure on line 41 in src/Behavior/EventBehavior.php

View workflow job for this annotation

GitHub Actions / larastan

Attribute class Spatie\LaravelData\Attributes\WithoutValidation does not have the parameter target.

Check failure on line 41 in src/Behavior/EventBehavior.php

View workflow job for this annotation

GitHub Actions / larastan

Attribute class Spatie\LaravelData\Attributes\WithoutValidation does not have the parameter target.
bool $isTransactional = null,
?bool $isTransactional = null,
#[WithoutValidation]

Check failure on line 43 in src/Behavior/EventBehavior.php

View workflow job for this annotation

GitHub Actions / larastan

Attribute class Spatie\LaravelData\Attributes\WithoutValidation does not have the parameter target.

Check failure on line 43 in src/Behavior/EventBehavior.php

View workflow job for this annotation

GitHub Actions / larastan

Attribute class Spatie\LaravelData\Attributes\WithoutValidation does not have the parameter target.
mixed $actor = null,
public int|Optional $version = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Behavior/GuardBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ abstract class GuardBehavior extends InvokableBehavior
abstract public function __invoke(
ContextManager $context,
EventBehavior $eventBehavior,
array $arguments = null,
?array $arguments = null,
): bool;
}
2 changes: 1 addition & 1 deletion src/Behavior/InvokableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
abstract public function __invoke(
ContextManager $context,
EventBehavior $eventBehavior,
array $arguments = null,
?array $arguments = null,
);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Behavior/ResultBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ abstract class ResultBehavior extends InvokableBehavior
abstract public function __invoke(
ContextManager $context,
EventBehavior $eventBehavior,
array $arguments = null,
?array $arguments = null,
): mixed;
}
2 changes: 1 addition & 1 deletion src/Behavior/ValidationGuardBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ abstract class ValidationGuardBehavior extends GuardBehavior
abstract public function __invoke(
ContextManager $context,
EventBehavior $eventBehavior,
array $arguments = null,
?array $arguments = null,
): bool;
}
2 changes: 1 addition & 1 deletion src/ContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function set(string $key, mixed $value): mixed
* @return bool True if the key exists and (if a type is specified)
* its value is of the given type. False otherwise.
*/
public function has(string $key, string $type = null): bool
public function has(string $key, ?string $type = null): bool
{
$hasKey = match (true) {
get_class($this) === self::class => Arr::has($this->data, $key),
Expand Down
22 changes: 11 additions & 11 deletions src/Definition/MachineDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ private function __construct(
* @return self The created machine definition.
*/
public static function define(
array $config = null,
array $behavior = null,
array $scenarios = null,
?array $config = null,
?array $behavior = null,
?array $scenarios = null,
): self {
return new self(
config: $config ?? null,
Expand Down Expand Up @@ -203,7 +203,7 @@ protected function createScenarioStateDefinitions(): void
*
* @return ?State The initial state of the machine.
*/
public function getInitialState(EventBehavior|array $event = null): ?State
public function getInitialState(EventBehavior|array|null $event = null): ?State
{
if (is_null($this->initialStateDefinition)) {
return null;
Expand Down Expand Up @@ -275,7 +275,7 @@ public function getInitialState(EventBehavior|array $event = null): ?State
*
* @return State|null The scenario state if scenario is enabled and found, otherwise returns the current state.
*/
public function getScenarioStateIfAvailable(State $state, EventBehavior|array $eventBehavior = null): ?State
public function getScenarioStateIfAvailable(State $state, EventBehavior|array|null $eventBehavior = null): ?State
{
if ($this->scenariosEnabled === false) {
return $state;
Expand Down Expand Up @@ -310,8 +310,8 @@ public function getScenarioStateIfAvailable(State $state, EventBehavior|array $e
*/
protected function buildCurrentState(
ContextManager $context,
StateDefinition $currentStateDefinition = null,
EventBehavior $eventBehavior = null,
?StateDefinition $currentStateDefinition = null,
?EventBehavior $eventBehavior = null,
): State {
return new State(
context: $context,
Expand Down Expand Up @@ -348,7 +348,7 @@ protected function getCurrentStateDefinition(string|State|null $state): mixed
*
* @return ContextManager The initialized context manager
*/
public function initializeContextFromState(State $state = null): ContextManager
public function initializeContextFromState(?State $state = null): ContextManager
{
// If a state is provided, use it's context
if (!is_null($state)) {
Expand Down Expand Up @@ -513,7 +513,7 @@ public function checkFinalStatesForTransitions(): void
protected function findTransitionDefinition(
StateDefinition $currentStateDefinition,
EventBehavior $eventBehavior,
string $firstStateDefinitionId = null,
?string $firstStateDefinitionId = null,
): ?TransitionDefinition {
$transitionDefinition = $currentStateDefinition->transitionDefinitions[$eventBehavior->type] ?? null;

Expand Down Expand Up @@ -552,7 +552,7 @@ protected function findTransitionDefinition(
*/
public function transition(
EventBehavior|array $event,
State $state = null
?State $state = null
): State {
if ($state !== null) {
$state = $this->getScenarioStateIfAvailable(state: $state, eventBehavior: $event);
Expand Down Expand Up @@ -688,7 +688,7 @@ public function transition(
public function runAction(
string $actionDefinition,
State $state,
EventBehavior $eventBehavior = null
?EventBehavior $eventBehavior = null
): void {
[$actionDefinition, $actionArguments] = array_pad(explode(':', $actionDefinition, 2), 2, null);
$actionArguments = $actionArguments === null ? [] : explode(',', $actionArguments);
Expand Down
4 changes: 2 additions & 2 deletions src/Definition/StateDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class StateDefinition
*/
public function __construct(
public ?array $config,
array $options = null,
?array $options = null,
) {
$this->initializeOptions($options);

Expand Down Expand Up @@ -457,7 +457,7 @@ public function runExitActions(State $state): void
*
* @param \Tarfinlabs\EventMachine\Behavior\EventBehavior|null $eventBehavior The event to be processed.
*/
public function runEntryActions(State $state, EventBehavior $eventBehavior = null): void
public function runEntryActions(State $state, ?EventBehavior $eventBehavior = null): void
{
// Record state entry start event
$state->setInternalEventBehavior(
Expand Down
2 changes: 1 addition & 1 deletion src/Definition/TransitionBranch.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function initializeInlineBehaviors(array $inlineBehaviors, BehaviorTyp
*/
public function runActions(
State $state,
EventBehavior $eventBehavior = null
?EventBehavior $eventBehavior = null
): void {
if ($this->actions === null) {
return;
Expand Down
7 changes: 0 additions & 7 deletions src/Definition/TransitionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ public function getFirstValidTransitionBranch(

$shouldLog = $guardBehavior?->shouldLog ?? false;

// Record the internal guard init event.
$state->setInternalEventBehavior(
type: InternalEvent::GUARD_START,
placeholder: $guardDefinition,
shouldLog: $shouldLog,
);

if ($guardBehavior instanceof GuardBehavior) {
$guardBehavior->validateRequiredContext($state->context);
}
Expand Down
8 changes: 3 additions & 5 deletions src/Enums/InternalEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ enum InternalEvent: string

case ACTION_START = '{machine}.action.{placeholder}.start';
case ACTION_FINISH = '{machine}.action.{placeholder}.finish';

case GUARD_START = '{machine}.guard.{placeholder}.start';
case GUARD_PASS = '{machine}.guard.{placeholder}.pass';
case GUARD_FAIL = '{machine}.guard.{placeholder}.fail';
case GUARD_PASS = '{machine}.guard.{placeholder}.pass';
case GUARD_FAIL = '{machine}.guard.{placeholder}.fail';

case EVENT_RAISED = '{machine}.event.{placeholder}.raised';

Expand All @@ -44,7 +42,7 @@ enum InternalEvent: string
*
* @return string The generated internal event name.
*/
public function generateInternalEventName(string $machineId, string $placeholder = null): string
public function generateInternalEventName(string $machineId, ?string $placeholder = null): string
{
if ($placeholder !== null && class_exists($placeholder)) {
$placeholder = Str::of($placeholder)->classBasename()->toString();
Expand Down
2 changes: 1 addition & 1 deletion tests/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
public function __invoke(
ContextManager $context,
EventBehavior $eventBehavior,
array $arguments = null
?array $arguments = null
): int {
return $eventBehavior->payload['value'] * $eventBehavior->payload['value'];
}
Expand Down
1 change: 0 additions & 1 deletion tests/EventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
'in.state.active.entry.finish',
'MUT',
'in.transition.active.MUT.start',
'in.guard.isEvenGuard.start',
'in.guard.isEvenGuard.fail',
'in.transition.active.MUT.fail',
]);
Expand Down
3 changes: 0 additions & 3 deletions tests/HistoryValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
'machine.state.green.entry.finish',
'TIMER',
'machine.transition.green.TIMER.start',
'machine.guard.IsTimerValidValidationGuard.start',
'machine.guard.IsTimerValidValidationGuard.fail',
'machine.transition.green.TIMER.fail',
]);
Expand All @@ -56,12 +55,10 @@
'machine.state.green.entry.finish',
'TIMER',
'machine.transition.green.TIMER.start',
'machine.guard.IsTimerValidValidationGuard.start',
'machine.guard.IsTimerValidValidationGuard.fail',
'machine.transition.green.TIMER.fail',
'TIMER',
'machine.transition.green.TIMER.start',
'machine.guard.IsTimerValidValidationGuard.start',
'machine.guard.IsTimerValidValidationGuard.pass',
'machine.transition.green.TIMER.finish',
'machine.state.green.exit.start',
Expand Down
6 changes: 3 additions & 3 deletions tests/InvokableBehaviorArgumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
behavior: [
'actions' => [
'additionAction' => function (ContextManager $context, EventDefinition $eventDefinition, array $arguments = null): void {
'additionAction' => function (ContextManager $context, EventDefinition $eventDefinition, ?array $arguments = null): void {
$context->count += array_sum($arguments);
},
],
Expand Down Expand Up @@ -57,12 +57,12 @@
],
behavior: [
'actions' => [
'additionAction' => function (ContextManager $context, EventDefinition $eventDefinition, array $arguments = null): void {
'additionAction' => function (ContextManager $context, EventDefinition $eventDefinition, ?array $arguments = null): void {
$context->count += array_sum($arguments);
},
],
'guards' => [
'biggerThan' => function (ContextManager $context, EventDefinition $eventDefinition, array $arguments = null): bool {
'biggerThan' => function (ContextManager $context, EventDefinition $eventDefinition, ?array $arguments = null): bool {
return $context->count > $arguments[0];
},
],
Expand Down
2 changes: 1 addition & 1 deletion tests/StateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
test('Logs if log writing is turned on', function (): void {
$machine = TrafficLightsMachine::create();

Log::shouldReceive('debug')->times(4);
Log::shouldReceive('debug')->times(3);

$machine->send(event: ['type' => 'MUT']);
});
2 changes: 1 addition & 1 deletion tests/Stubs/Actions/IsOddAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IsOddAction extends ActionBehavior
'counts.oddCount' => 'integer',
];

public function __invoke(ContextManager $context, EventBehavior $eventBehavior, array $arguments = null): void
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void
{
$context->set('counts.oddCount', 1);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Guards/IsOddGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class IsOddGuard extends GuardBehavior
public function __invoke(
TrafficLightsContext|ContextManager $context,
EventBehavior $eventBehavior,
array $arguments = null
?array $arguments = null
): bool {
return $context->count % 2 === 1;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Guards/IsTimerValidValidationGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Guards/IsValidatedOddGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class IsValidatedOddGuard extends ValidationGuardBehavior
public function __invoke(
TrafficLightsContext|ContextManager $context,
EventBehavior $eventBehavior,
array $arguments = null
?array $arguments = null
): bool {
return $context->count % 2 === 1;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Machines/Asd/Actions/AAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class AAction extends ActionBehavior
{
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, array $arguments = null): void
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void
{
ModelA::create([
'value' => 'lorem ipsum dolor',
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Machines/Asd/Actions/DAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DAction extends ActionBehavior
/**
* @throws \Exception
*/
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, array $arguments = null): void
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void
{
throw new \Exception('error');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Machines/Asd/Actions/SAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SAction extends ActionBehavior
/**
* @throws \Exception
*/
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, array $arguments = null): void
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void
{
ModelA::first()->update([
'value' => 'new value',
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Machines/Asd/Actions/SleepAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SleepAction extends ActionBehavior
/**
* @throws \Exception
*/
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, array $arguments = null): void
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void
{
sleep(1);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Machines/Qwerty/Actions/TAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class TAction extends ActionBehavior
{
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, array $arguments = null): void
public function __invoke(ContextManager $context, EventBehavior $eventBehavior, ?array $arguments = null): void
{
$this->raise(new TEvent(actor: $eventBehavior->actor($context)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class AddAnotherValueAction extends ActionBehavior
{
public function __invoke(ContextManager|TrafficLightsContext $context, EventBehavior|AddAnotherValueEvent $eventBehavior, array $arguments = null): void
public function __invoke(ContextManager|TrafficLightsContext $context, EventBehavior|AddAnotherValueEvent $eventBehavior, ?array $arguments = null): void
{
$context->count += $eventBehavior->value;
}
Expand Down
Loading

0 comments on commit 1ce5b93

Please sign in to comment.