Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Fsm Laravel Package

christhompsontldr/laravel-fsm

View on GitHub
Deep Wiki
Context7

Documentation: TransitionMetric.php

Original file: src/Fsm/Events/TransitionMetric.php

TransitionMetric Documentation

Table of Contents

Introduction

The TransitionMetric class is part of the Fsm\Events namespace within a Laravel application that implements a finite state machine (FSM). This class is designed to encapsulate transition metrics that are recorded when transitioning between different states within the FSM. It is primarily used for tracking and analyzing state changes in a way that can be integrated with other parts of the system, such as logging, metrics gathering, or triggering additional business logic in response to state transitions.

Class Overview

TransitionMetric

The TransitionMetric class serves as a data transfer object (DTO) to facilitate the structured passing of transition state data within the finite state machine context. Below is the detailed documentation of its constructor used to initialize the metrics.

Constructor

public function __construct(
    public readonly Model $model,
    public readonly string $columnName,
    public readonly FsmStateEnum|string|null $fromState,
    public readonly FsmStateEnum|string $toState,
    public readonly bool $successful,
    public readonly ?ArgonautDTOContract $context = null,
)

Purpose

The constructor is responsible for creating an instance of the TransitionMetric class with the necessary attributes that define a transition in the FSM.

Parameters

Parameter Type Description
$model Model An instance of an Eloquent model that represents the entity undergoing the state transition.
$columnName string The name of the database column that corresponds to the state being transitioned.
$fromState `FsmStateEnum string
$toState `FsmStateEnum string`
$successful bool A boolean indicating whether the state transition was successful.
$context `ArgonautDTOContract null`

Functionality

The TransitionMetric constructor initializes the object with the provided parameters. It ensures encapsulation of the transition's relevant data, promoting immutability through the use of readonly properties. This design choice encourages safer programming practices by ensuring that the state metrics remain unchanged after instantiation.

All parameters are type-hinted, enforcing type safety and improving code reliability. The context parameter is optional, which allows flexibility in scenarios where no additional context is necessary.

Example Usage

Here’s an example of how to create a new instance of the TransitionMetric class:

use Fsm\Events\TransitionMetric;
use App\Models\User; // Example model
use Fsm\Contracts\FsmStateEnum;

$user = new User(); // Assume this is a model instance
$columnName = 'current_state';
$fromState = FsmStateEnum::INACTIVE; // Previously defined state
$toState = FsmStateEnum::ACTIVE;      // Transitioning to a new state
$successful = true;
$context = null; // No additional context

$transitionMetric = new TransitionMetric($user, $columnName, $fromState, $toState, $successful, $context);

This example illustrates how the TransitionMetric can be used to track state transitions for a user model within the FSM, conveying critical information about the transition process.

The TransitionMetric class plays a vital role in ensuring that information regarding state changes is recorded consistently, allowing for easier auditing, logging, and handling of business logic associated with state management in this application's context.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport