Skip to content

Commit

Permalink
direct use of instance instead of class name of model
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Bigelmayr committed Nov 26, 2024
1 parent 1bb3600 commit 795288a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
5 changes: 2 additions & 3 deletions app/ModelStates/ModelStates/ModelState.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public static function defaultState(): ModelState
return new StateA();
}

/** @return class-string<Model> */
public static function stateManagerClass(): string
public static function stateManagerClass(): Model
{
return StateManager::class;
return new StateManager();
}
}
5 changes: 2 additions & 3 deletions app/ModelStates/OtherModelStates/OtherModelState.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public static function defaultState(): OtherModelState
return new StateX();
}

/** @return class-string<Model> */
public static function stateManagerClass(): string
public static function stateManagerClass(): Model
{
return StateManager::class;
return new StateManager();
}
}
7 changes: 3 additions & 4 deletions src/ModelStates/HasStateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public static function bootHasStateManager(): void
return;
}

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

Expand All @@ -29,9 +28,9 @@ public static function bootHasStateManager(): void

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

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

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

abstract public static function defaultState(): self;

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

public static function name(): string
{
Expand Down

0 comments on commit 795288a

Please sign in to comment.