2026-02-04 23:31:53 +01:00
< ? php
2026-02-11 19:28:12 +01:00
namespace MintyPHP\Repository\Auth ;
2026-02-04 23:31:53 +01:00
use MintyPHP\DB ;
2026-03-13 21:58:51 +01:00
/** Stores email verification codes with attempt counters, expiry, and completion status. */
2026-03-05 08:26:51 +01:00
class EmailVerificationRepository implements EmailVerificationRepositoryInterface
2026-02-04 23:31:53 +01:00
{
2026-02-23 12:58:19 +01:00
public function create ( int $userId , string $codeHash , string $expiresAt ) : ? int
2026-02-04 23:31:53 +01:00
{
$id = DB :: insert (
'insert into email_verifications (user_id, code_hash, expires_at, attempts, used_at, created) values (?,?,?,?,NULL,NOW())' ,
( string ) $userId ,
$codeHash ,
$expiresAt ,
0
);
return $id ? ( int ) $id : null ;
}
2026-02-23 12:58:19 +01:00
public function invalidateForUser ( int $userId ) : bool
2026-02-04 23:31:53 +01:00
{
$result = DB :: update (
'update email_verifications set used_at = NOW() where user_id = ? and used_at is null' ,
( string ) $userId
);
return $result !== false ;
}
2026-02-23 12:58:19 +01:00
public function findActiveByUserId ( int $userId ) : ? array
2026-02-04 23:31:53 +01:00
{
$row = DB :: selectOne (
'select id, user_id, code_hash, expires_at, attempts, used_at from email_verifications where user_id = ? and used_at is null and expires_at > UTC_TIMESTAMP() order by id desc limit 1' ,
( string ) $userId
);
if ( ! $row || ! isset ( $row [ 'email_verifications' ])) {
return null ;
}
return $row [ 'email_verifications' ];
}
2026-02-23 12:58:19 +01:00
public function findById ( int $id ) : ? array
2026-02-04 23:31:53 +01:00
{
$row = DB :: selectOne (
'select id, user_id, code_hash, expires_at, attempts, used_at from email_verifications where id = ? limit 1' ,
( string ) $id
);
if ( ! $row || ! isset ( $row [ 'email_verifications' ])) {
return null ;
}
return $row [ 'email_verifications' ];
}
2026-02-23 12:58:19 +01:00
public function incrementAttempts ( int $id ) : bool
2026-02-04 23:31:53 +01:00
{
$result = DB :: update (
'update email_verifications set attempts = attempts + 1 where id = ?' ,
( string ) $id
);
return $result !== false ;
}
2026-02-23 12:58:19 +01:00
public function markUsed ( int $id ) : bool
2026-02-04 23:31:53 +01:00
{
$result = DB :: update (
'update email_verifications set used_at = NOW() where id = ?' ,
( string ) $id
);
return $result !== false ;
}
}