Skip to content

Commit 73d147d

Browse files
committed
refactor: change return type to void
1 parent 5d7ec18 commit 73d147d

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/Models/RememberModel.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodeIgniter\Shield\Models;
44

55
use CodeIgniter\Model;
6+
use CodeIgniter\Shield\Exceptions\RuntimeException;
67
use DateTime;
78
use stdClass;
89

@@ -24,25 +25,36 @@ class RememberModel extends Model
2425
* Stores a remember-me token for the user.
2526
*
2627
* @param int|string $userId
27-
*
28-
* @return mixed
2928
*/
30-
public function rememberUser($userId, string $selector, string $hashedValidator, string $expires)
29+
public function rememberUser($userId, string $selector, string $hashedValidator, string $expires): void
3130
{
3231
$expires = new DateTime($expires);
3332

34-
return $this->insert([
33+
$return = $this->insert([
3534
'user_id' => $userId,
3635
'selector' => $selector,
3736
'hashedValidator' => $hashedValidator,
3837
'expires' => $expires->format('Y-m-d H:i:s'),
3938
]);
39+
40+
$this->checkQueryReturn($return);
41+
}
42+
43+
private function checkQueryReturn(bool $return): void
44+
{
45+
if ($return === false) {
46+
$error = $this->db->error();
47+
$message = 'Query error: ' . $error['code'] . ', '
48+
. $error['message'] . ', query: ' . $this->db->getLastQuery();
49+
50+
throw new RuntimeException($message, $error['code']);
51+
}
4052
}
4153

4254
/**
4355
* Returns the remember-me token info for a given selector.
4456
*
45-
* @return mixed
57+
* @return stdClass|null
4658
*/
4759
public function getRememberToken(string $selector)
4860
{
@@ -53,29 +65,25 @@ public function getRememberToken(string $selector)
5365

5466
/**
5567
* Updates the validator for a given selector.
56-
*
57-
* @return mixed
5868
*/
59-
public function updateRememberValidator(stdClass $token)
69+
public function updateRememberValidator(stdClass $token): void
6070
{
61-
return $this->save($token);
71+
$return = $this->save($token);
72+
73+
$this->checkQueryReturn($return);
6274
}
6375

6476
/**
6577
* Removes all persistent login tokens (remember-me) for a single user
6678
* across all devices they may have logged in with.
6779
*
68-
* @param int|string|null $userId
69-
*
70-
* @return mixed
80+
* @param int|string $userId
7181
*/
72-
public function purgeRememberTokens($userId = null)
82+
public function purgeRememberTokens($userId): void
7383
{
74-
if (empty($userId)) {
75-
return;
76-
}
84+
$return = $this->where(['user_id' => $userId])->delete();
7785

78-
return $this->where(['user_id' => $userId])->delete();
86+
$this->checkQueryReturn($return);
7987
}
8088

8189
/**
@@ -84,7 +92,9 @@ public function purgeRememberTokens($userId = null)
8492
*/
8593
public function purgeOldRememberTokens(): void
8694
{
87-
$this->where('expires <=', date('Y-m-d H:i:s'))
95+
$return = $this->where('expires <=', date('Y-m-d H:i:s'))
8896
->delete();
97+
98+
$this->checkQueryReturn($return);
8999
}
90100
}

0 commit comments

Comments
 (0)