Skip to content

Commit 2f6bb67

Browse files
committed
refactor: move lastLogin() implementation to LoginModel
1 parent d22158f commit 2f6bb67

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/Entities/User.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,13 @@ public function getPasswordHash(): ?string
224224
}
225225

226226
/**
227-
* Returns the last login information for this
228-
* user as
227+
* Returns the last login information for this user as
229228
*/
230229
public function lastLogin(bool $allowFailed = false): ?Login
231230
{
231+
/** @var LoginModel $logins */
232232
$logins = model(LoginModel::class);
233233

234-
if (! $allowFailed) {
235-
$logins->where('success', 1)
236-
->where('user_id', $this->attributes['id'] ?? null);
237-
}
238-
239-
return $logins
240-
->where('identifier', $this->getEmail())
241-
->orderBy('date', 'desc')
242-
->first();
234+
return $logins->lastLogin($this, $allowFailed);
243235
}
244236
}

src/Models/LoginModel.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use CodeIgniter\I18n\Time;
66
use CodeIgniter\Model;
77
use CodeIgniter\Shield\Entities\Login;
8+
use CodeIgniter\Shield\Entities\User;
89
use Exception;
910
use Faker\Generator;
1011

@@ -57,6 +58,22 @@ public function recordLoginAttempt(
5758
$this->checkQueryReturn($return);
5859
}
5960

61+
/**
62+
* Returns the last login information for the user
63+
*/
64+
public function lastLogin(User $user, bool $allowFailed = false): ?Login
65+
{
66+
if (! $allowFailed) {
67+
$this->where('success', 1)
68+
->where('user_id', $user->id);
69+
}
70+
71+
return $this
72+
->where('identifier', $user->email)
73+
->orderBy('date', 'desc')
74+
->first();
75+
}
76+
6077
/**
6178
* Generate a fake login for testing
6279
*

0 commit comments

Comments
 (0)