Skip to content

Commit 7ba3c27

Browse files
feat: add resolveLockoutService for better extendibility
1 parent 4a26a1a commit 7ba3c27

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/Traits/HasLockout.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Beliven\Lockout\Traits;
44

5-
use Beliven\Lockout\Facades\Lockout;
65
use Beliven\Lockout\Models\ModelLockout;
76
use Illuminate\Database\Eloquent\Relations\MorphMany;
87

@@ -67,6 +66,18 @@ public function hasActiveLock(): bool
6766
->exists();
6867
}
6968

69+
/**
70+
* Resolve the Lockout service instance.
71+
*
72+
* This method exists so consumers and tests can override the returned
73+
* implementation (for example by extending the model and overriding this
74+
* method). By default it resolves the package service from the container.
75+
*/
76+
protected function resolveLockoutService(): \Beliven\Lockout\Lockout
77+
{
78+
return app(\Beliven\Lockout\Lockout::class);
79+
}
80+
7081
/**
7182
* Determine if the model is considered locked.
7283
*
@@ -81,14 +92,15 @@ public function isLockedOut(): bool
8192
}
8293

8394
// Fallback to the lockout service's attempt counter.
84-
$identifier = Lockout::getLoginField();
95+
$service = $this->resolveLockoutService();
96+
$identifier = $service->getLoginField();
8597

8698
// Guard in case the model does not expose the configured login field.
8799
if (!isset($this->{$identifier})) {
88100
return false;
89101
}
90102

91-
return Lockout::hasTooManyAttempts((string) $this->{$identifier});
103+
return $service->hasTooManyAttempts((string) $this->{$identifier});
92104
}
93105

94106
/**
@@ -106,7 +118,7 @@ public function isLockedOut(): bool
106118
*/
107119
public function lock(array $options = []): ModelLockout
108120
{
109-
return Lockout::lockModel($this, $options);
121+
return $this->resolveLockoutService()->lockModel($this, $options);
110122
}
111123

112124
/**
@@ -122,6 +134,6 @@ public function lock(array $options = []): ModelLockout
122134
*/
123135
public function unlock(array $options = []): ?ModelLockout
124136
{
125-
return Lockout::unlockModel($this, $options);
137+
return $this->resolveLockoutService()->unlockModel($this, $options);
126138
}
127139
}

0 commit comments

Comments
 (0)