Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhemphill committed Dec 17, 2024
1 parent 2b95d6a commit 7d5e516
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ composer require hemp/machinery

## Usage

First, create an enumeration that represents the states for your Eloquent model and the valid transitions between them:
First, create an enumeration that represents the states you wish to
model and the valid transitions between them:

```php
use Hemp\Machinery\MachineState;
use Hemp\Machinery\MachineStateTrait;
use Hemp\Machinery\MachineryState;
use Hemp\Machinery\MachineryEnumeration;

enum OrderStatus: string implements MachineState
enum OrderStatus: string implements MachineryState
{
use MachineStateTrait;
use MachineryEnumeration;

case Processing : 'processing';
case Shipped : 'shipped';
Expand All @@ -48,6 +49,24 @@ enum OrderStatus: string implements MachineState
}
```

Now you can use the `OrderStatus` enumeration to manage transitions
between states:

```php
use Hemp\Machinery\Machinery;

OrderStatus::Processing->canTransitionTo(OrderStatus::Shipped); // true
OrderStatus::Processing->transitionTo(OrderStatus::Shipped); // state is now 'shipped'

OrderStatus::Shipped->canTransitionTo(OrderStatus::Delivered); // true
OrderStatus::Shipped->transitionTo(OrderStatus::Delivered); // state is now 'delivered'

OrderStatus::Delivered->canTransitionTo(OrderStatus::Processing); //
OrderStatus::Delivered->transitionTo(OrderStatus::Processing); // Throws an exception...
```

## Using the state machine with Eloquent

Next, add a column to your Eloquent model's `casts` to store the state:

```php
Expand Down
12 changes: 0 additions & 12 deletions src/MachineState.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Machinery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

trait Machinery
{
public function transitionTo($machineKey, MachineState $state, ?callable $sideEffect = null): MachineState
public function transitionTo($machineKey, MachineryState $state, ?callable $sideEffect = null): MachineryState
{
$sideEffect = $sideEffect ?? fn () => null;

Expand All @@ -16,7 +16,7 @@ public function transitionTo($machineKey, MachineState $state, ?callable $sideEf
return $this->{$machineKey}->transitionTo($state, $callable);
}

public function canTransitionTo($machineKey, MachineState $state): bool
public function canTransitionTo($machineKey, MachineryState $state): bool
{
return $this->{$machineKey}->canTransitionTo($state);
}
Expand Down
13 changes: 6 additions & 7 deletions src/MachineStateTrait.php → src/MachineryEnumeration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@
/**
* @method static transitions()
*/
trait MachineStateTrait
trait MachineryEnumeration
{
/**
* @throws InvalidStateTransition
*/
public function transitionTo(MachineState $state, ?callable $sideEffect = null): MachineState
public function transitionTo(MachineryState $state, ?callable $sideEffect = null): MachineryState
{
$callback = $sideEffect ?? fn () => null;

if (! $this->canTransitionTo($state)) {
if (!$this->canTransitionTo($state)) {
throw new InvalidStateTransition("Cannot transition from state [{$this->value}] to state [{$state->value}].");
}

$callback();
call_user_func($sideEffect ?? fn() => null);

return $state;
}

public function canTransitionTo(MachineState $state): bool
public function canTransitionTo(MachineryState $state): bool
{
return in_array($state, self::transitions()[$this->value]);
}

public function is(MachineState $state): bool
public function is(MachineryState $state): bool
{
return $this->value === $state->value;
}
Expand Down
12 changes: 12 additions & 0 deletions src/MachineryState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Hemp\Machinery;

interface MachineryState
{
public function transitionTo(MachineryState $state, callable $sideEffect): MachineryState;

public static function transitions(): array;

public function canTransitionTo(MachineryState $state): bool;
}
8 changes: 4 additions & 4 deletions tests/Fixtures/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Hemp\Machinery\Tests\Fixtures;

use Hemp\Machinery\MachineState;
use Hemp\Machinery\MachineStateTrait;
use Hemp\Machinery\MachineryState;
use Hemp\Machinery\MachineryEnumeration;

enum Status: string implements MachineState
enum Status: string implements MachineryState
{
use MachineStateTrait;
use MachineryEnumeration;

case Active = 'active';

Expand Down

0 comments on commit 7d5e516

Please sign in to comment.