From e8725476f11ee6113b78c32f04f191b02a1c08b7 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 26 Nov 2024 15:24:14 +0100 Subject: [PATCH] avoid unnecessary instantiation --- app/ModelStates/ModelStates/ModelState.php | 5 +-- .../OtherModelStates/OtherModelState.php | 5 +-- src/ModelStates/HasStateManager.php | 40 +++++++++++-------- src/ModelStates/State.php | 3 +- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/ModelStates/ModelStates/ModelState.php b/app/ModelStates/ModelStates/ModelState.php index d14e128..dd7b426 100644 --- a/app/ModelStates/ModelStates/ModelState.php +++ b/app/ModelStates/ModelStates/ModelState.php @@ -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; @@ -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; } } diff --git a/app/ModelStates/OtherModelStates/OtherModelState.php b/app/ModelStates/OtherModelStates/OtherModelState.php index 6371d9a..6dd06ab 100644 --- a/app/ModelStates/OtherModelStates/OtherModelState.php +++ b/app/ModelStates/OtherModelStates/OtherModelState.php @@ -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; @@ -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; } } diff --git a/src/ModelStates/HasStateManager.php b/src/ModelStates/HasStateManager.php index f2162ac..d9c0278 100644 --- a/src/ModelStates/HasStateManager.php +++ b/src/ModelStates/HasStateManager.php @@ -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); @@ -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 $newState */ @@ -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 diff --git a/src/ModelStates/State.php b/src/ModelStates/State.php index c216a5e..3bbf52a 100644 --- a/src/ModelStates/State.php +++ b/src/ModelStates/State.php @@ -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 */ + abstract public static function stateManagerClass(): string; public static function name(): string {