Skip to content

Commit

Permalink
avoid unnecessary instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Nov 26, 2024
1 parent 9f074f0 commit e872547
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
5 changes: 2 additions & 3 deletions app/ModelStates/ModelStates/ModelState.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\ModelStates\ModelStates;

use App\ModelStates\StateManager;
use Illuminate\Database\Eloquent\Model;
use MLL\LaravelUtils\ModelStates\State;
use MLL\LaravelUtils\ModelStates\StateConfig;

Expand All @@ -22,8 +21,8 @@ public static function defaultState(): ModelState
return new StateA();
}

public static function stateManagerClass(): Model
public static function stateManagerClass(): string
{
return new StateManager();
return StateManager::class;
}
}
5 changes: 2 additions & 3 deletions app/ModelStates/OtherModelStates/OtherModelState.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\ModelStates\StateManager;
use App\ModelStates\Transitions\CustomInvalidTransition;
use App\ModelStates\Transitions\CustomTransition;
use Illuminate\Database\Eloquent\Model;
use MLL\LaravelUtils\ModelStates\State;
use MLL\LaravelUtils\ModelStates\StateConfig;

Expand All @@ -23,8 +22,8 @@ public static function defaultState(): OtherModelState
return new StateX();
}

public static function stateManagerClass(): Model
public static function stateManagerClass(): string
{
return new StateManager();
return StateManager::class;
}
}
40 changes: 23 additions & 17 deletions src/ModelStates/HasStateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ abstract public function stateClass(): string;
public static function bootHasStateManager(): void
{
self::created(function (self $self): void {
if (property_exists($self, 'stateManager') && $self->stateManager !== null) {
if (property_exists($self, 'stateManager')
&& isset($self->stateManager)
) {
return;
}

$stateManager = $self->stateClass()::stateManagerClass();
assert(in_array(IsStateManager::class, class_uses($stateManager)));
$stateManager->setAttribute($stateManager::stateColumnName(), $self->stateClass()::defaultState()::name());
$stateClass = $self->stateClass();

$stateManagerClass = $stateClass::stateManagerClass();
assert(in_array(IsStateManager::class, class_uses($stateManagerClass)));

$stateManager = new $stateManagerClass();

$defaultState = $stateClass::defaultState();
$stateManager->setAttribute($stateManager::stateColumnName(), $defaultState::name());

$self->stateManager()->save($stateManager);
$self->setRelation('stateManager', $stateManager);
Expand All @@ -28,19 +36,16 @@ public static function bootHasStateManager(): void

public function getStateAttribute(): State
{
$stateManager = self::stateClass()::stateManagerClass();
assert(in_array(IsStateManager::class, class_uses($stateManager)));

$stateName = $this->stateManager->getAttribute($stateManager::stateColumnName());
$stateName = $this->stateManager->getAttribute($this->stateManager::stateColumnName());
assert(is_string($stateName));

$stateClasses = $this->stateClassConfig()->possibleStates();
$stateClass = $stateClasses[$stateName] ?? null;
if ($stateClass === null) {
$possibleStateClasses = $this->stateClassConfig()->possibleStates();
$currentStateClass = $possibleStateClasses[$stateName] ?? null;
if ($currentStateClass === null) {
throw new UnknownStateException("The state {$stateName} of {$this->table} with id {$this->id} is not part of {$this->stateClass()}.");
}

return new $stateClass();
return new $currentStateClass();
}

/** @param State|class-string<State> $newState */
Expand All @@ -57,15 +62,16 @@ public function setStateAttribute(State|string $newState): void

public function stateManager(): MorphOne
{
return $this->morphOne(
$this->stateClass()::stateManagerClass(),
'stateable',
);
$stateClass = $this->stateClass();

return $this->morphOne($stateClass::stateManagerClass(), 'stateable');
}

public function stateClassConfig(): StateConfig
{
return $this->stateClass()::config();
$stateClass = $this->stateClass();

return $stateClass::config();
}

public function stateMachine(): StateMachine
Expand Down
3 changes: 2 additions & 1 deletion src/ModelStates/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ abstract public static function config(): StateConfig;

abstract public static function defaultState(): self;

abstract public static function stateManagerClass(): Model;
/** @return class-string<Model> */
abstract public static function stateManagerClass(): string;

public static function name(): string
{
Expand Down

0 comments on commit e872547

Please sign in to comment.