Original file: src/Fsm/Services/FsmMetricsService.php
The FsmMetricsService class is part of the Fsm\Services namespace in the Laravel application and is designed to facilitate the tracking of state transitions within a finite state machine (FSM). It leverages Laravel's event dispatching system and caching mechanism to record successful and failed transitions of models, sending relevant metrics for further analysis and monitoring.
It is particularly useful in contexts where state transitions are critical and need to be monitored for performance, error handling, or user behavior analysis.
| Name | Type | Description |
|---|---|---|
| CACHE_KEY_SUCCESS | string | The cache key used to track successful transitions. |
| CACHE_KEY_FAILURE | string | The cache key used to track failed transitions. |
| dispatcher | Dispatcher | An instance of Laravel's event dispatcher for dispatching events. |
public function __construct(
private readonly Dispatcher $dispatcher
)
The constructor initializes the FsmMetricsService with a Dispatcher instance, which is essential for dispatching transition events.
Dispatcher $dispatcher: An instance of the Laravel event dispatcher that enables the service to send events related to state transitions.public function record(
Model $model,
string $columnName,
FsmStateEnum|string|null $fromState,
FsmStateEnum|string $toState,
bool $successful,
?ArgonautDTOContract $context = null
): void
Records a state transition from one FSM state to another, tracking whether the transition was successful or not. It also increments the appropriate cache key for statistics.
| Name | Type | Description |
|---|---|---|
$model |
Model | The Eloquent model whose state is being recorded. |
$columnName |
string | The name of the column representing the FSM state in the model. |
$fromState |
FsmStateEnum | string |
$toState |
FsmStateEnum | string |
$successful |
bool | Indicates whether the transition was successful (true) or failed (false). |
$context |
?ArgonautDTOContract | Optional context information related to the transition. |
Caching:
$successful parameter using predefined cache keys (CACHE_KEY_SUCCESS or CACHE_KEY_FAILURE).Event Dispatching:
TransitionMetric event with the details of the transition including the model, column name, states, success status, and any optional context provided.Error Handling:
TypeError.State Management:
Here's a brief example demonstrating how to use the FsmMetricsService:
use Fsm\Services\FsmMetricsService;
use SomeModel; // An Eloquent model
use SomeStateEnum; // An enum representing FSM states
$fsmMetricsService = new FsmMetricsService($dispatcher);
$model = new SomeModel();
$fsmMetricsService->record($model, 'state', null, SomeStateEnum::NEW, true);
In this example, we record a successful transition from no state (null) to SomeStateEnum::NEW for an instance of SomeModel.
The FsmMetricsService provides essential functionalities for recording and monitoring state transitions within a finite state machine in a Laravel application. By utilizing caching and event dispatching, it enhances the observability of state changes, thereby aiding developers in maintaining the health and performance of their FSM implementations.
How can I help you explore Laravel packages today?