Original file: src/Fsm/Services/FsmEngineService.php
The FsmEngineService class in this file is designed to provide a core engine for handling state transitions within a finite state machine (FSM). It validates and executes transitions, evaluates guard conditions, runs actions, and emits events related to state changes. This service facilitates a comprehensive environment for state machine operations, including logging, metrics collection, and error management.
The FsmEngineService class encapsulates the logic required to handle FSM operations in Laravel. It leverages various components including guards, callbacks, and actions to ensure transitions are executed correctly and according to defined rules.
public function __construct(
private readonly FsmRegistry $registry,
private readonly FsmLogger $logger,
private readonly FsmMetricsService $metrics,
private readonly DatabaseManager $db,
private readonly ConfigRepository $config
)
FsmRegistry $registry: The registry that holds definitions of FSMs.FsmLogger $logger: The service for logging FSM operations and transitions.FsmMetricsService $metrics: The service for recording metrics related to FSM transitions.DatabaseManager $db: Database management service to handle transactions.ConfigRepository $config: Configuration repository to retrieve settings.protected function getDefinition(string $modelClass, string $columnName): FsmRuntimeDefinition
Retrieves the FSM runtime definition for a given model class and column name.
string $modelClass: Fully qualified class name of the model.string $columnName: The name of the state column in the model.FsmRuntimeDefinition: The FSM definition corresponding to the specified model and column.LogicException if the definition is not found.public function getCurrentState(Model $model, string $columnName): FsmStateEnum|string|null
Obtains the current state of the FSM for a specified Eloquent model and state column.
Model $model: The Eloquent model instance whose state is being queried.string $columnName: The state column name.FsmStateEnum|string|null: The current state as an enum or string, or null if no initial state exists.public function canTransition(Model $model, string $columnName, FsmStateEnum|string $toState, ?ArgonautDTOContract $context = null): bool
Determines if a transition to a specified state can occur without executing the transition.
Model $model: The Eloquent model instance.string $columnName: The state column name.FsmStateEnum|string $toState: The target state for the transition.ArgonautDTOContract|null $context: Optional context for the transition.bool: true if the transition can succeed, false otherwise.public function dryRunTransition(Model $model, string $columnName, FsmStateEnum|string $toState, ?ArgonautDTOContract $context = null): array
Simulates a transition attempt, returning the outcome without actually transitioning the model state.
Model $model: The Eloquent model instance.string $columnName: The state column name.FsmStateEnum|string $toState: The intended target state.ArgonautDTOContract|null $context: Optional transition context.array: Contains details about the transition attempt, including:
can_transition: Whether the transition can occur.from_state: Current state before transition.to_state: Target state for the transition.message: Descriptive message about the attempt.reason: Reason for failure, if applicable.public function performTransition(Model $model, string $columnName, FsmStateEnum|string $toState, ?ArgonautDTOContract $context = null): Model
Performs a state transition for the specified model, updating the state in the database.
Model $model: The Eloquent model instance to transition.string $columnName: The state column name.FsmStateEnum|string $toState: The target state for the transition.ArgonautDTOContract|null $context: Optional context for the transition.Model: The updated model instance after the transition.protected function findTransition(FsmRuntimeDefinition $definition, FsmStateEnum|string|null $fromState, FsmStateEnum|string $toState): ?TransitionDefinition
Locates a transition definition based on the current state and target state.
FsmRuntimeDefinition $definition: The FSM definition containing possible transitions.FsmStateEnum|string|null $fromState: The state from which the transition is made.FsmStateEnum|string $toState: The target state for the transition.TransitionDefinition|null: The found transition definition or null if not found.How can I help you explore Laravel packages today?