DatabaseUserRepository
in package
implements
IUserRepository
uses
ManagesTimestamps
Database-backed user repository using ORM.
Works with SQLite, MySQL, and PostgreSQL via the Neuron ORM.
Table of Contents
Interfaces
- IUserRepository
- User repository interface.
Properties
- $_pdo : PDO|null
Methods
- __construct() : mixed
- Constructor
- __sleep() : array<string|int, mixed>
- Handle serialization for PHPUnit process isolation
- __wakeup() : void
- Handle unserialization for PHPUnit process isolation
- all() : array<string|int, mixed>
- Get all users
- count() : int
- Count total users
- create() : User
- Create a new user
- delete() : bool
- Delete a user
- findByEmail() : User|null
- Find user by email
- findById() : User|null
- Find user by ID
- findByRememberToken() : User|null
- Find user by remember token
- findByUsername() : User|null
- Find user by username
- incrementFailedLoginAttempts() : int
- Atomically increment failed login attempts for a user
- resetFailedLoginAttempts() : bool
- Atomically reset failed login attempts and unlock account
- setLockedUntil() : bool
- Atomically set account lockout until specified time
- update() : bool
- Update an existing user
- createEntity() : T
- Prepare entity for creation by setting timestamps, saving, and refreshing
- ensureTimestamps() : void
- Set created_at and updated_at timestamps if not already set
- saveAndRefresh() : T
- Save entity and return the refreshed version from database
Properties
$_pdo
private
PDO|null
$_pdo
= null
Methods
__construct()
Constructor
public
__construct(SettingManager $settings) : mixed
Parameters
- $settings : SettingManager
-
Settings manager with database configuration
Tags
__sleep()
Handle serialization for PHPUnit process isolation
public
__sleep() : array<string|int, mixed>
Return values
array<string|int, mixed>__wakeup()
Handle unserialization for PHPUnit process isolation
public
__wakeup() : void
all()
Get all users
public
all() : array<string|int, mixed>
Return values
array<string|int, mixed>count()
Count total users
public
count() : int
Return values
intcreate()
Create a new user
public
create(User $user) : User
Parameters
- $user : User
Return values
Userdelete()
Delete a user
public
delete(int $id) : bool
Parameters
- $id : int
Return values
boolfindByEmail()
Find user by email
public
findByEmail(string $email) : User|null
Parameters
- $email : string
Return values
User|nullfindById()
Find user by ID
public
findById(int $id) : User|null
Parameters
- $id : int
Return values
User|nullfindByRememberToken()
Find user by remember token
public
findByRememberToken(string $token) : User|null
Parameters
- $token : string
Return values
User|nullfindByUsername()
Find user by username
public
findByUsername(string $username) : User|null
Parameters
- $username : string
Return values
User|nullincrementFailedLoginAttempts()
Atomically increment failed login attempts for a user
public
incrementFailedLoginAttempts(int $userId) : int
Uses atomic UPDATE to avoid race condition under concurrent login attempts.
Parameters
- $userId : int
-
User ID
Return values
int —New failed login attempts count, or -1 if user not found
resetFailedLoginAttempts()
Atomically reset failed login attempts and unlock account
public
resetFailedLoginAttempts(int $userId) : bool
Uses atomic UPDATE to avoid race condition.
Parameters
- $userId : int
-
User ID
Return values
bool —True if successful, false if user not found
setLockedUntil()
Atomically set account lockout until specified time
public
setLockedUntil(int $userId, DateTimeImmutable|null $lockedUntil) : bool
Uses atomic UPDATE to avoid race condition.
Parameters
- $userId : int
-
User ID
- $lockedUntil : DateTimeImmutable|null
-
Locked until time, or null to unlock
Return values
bool —True if successful, false if user not found
update()
Update an existing user
public
update(User $user) : bool
Parameters
- $user : User
Return values
boolcreateEntity()
Prepare entity for creation by setting timestamps, saving, and refreshing
protected
createEntity(T $entity, callable $finder, string $entityType) : T
This combines the common pattern of:
- Setting timestamps if not already set
- Saving the entity
- Re-fetching from database to get all DB-generated values
Use this in create() methods after performing duplicate checks.
Parameters
- $entity : T
-
Entity to create
- $finder : callable
-
Callback to find entity by ID: function(int $id): ?T
- $entityType : string
-
Human-readable entity type for error messages
Tags
Return values
T —The refreshed entity from database
ensureTimestamps()
Set created_at and updated_at timestamps if not already set
protected
ensureTimestamps(object $entity) : void
Parameters
- $entity : object
-
Entity with getCreatedAt/setCreatedAt and getUpdatedAt/setUpdatedAt methods
saveAndRefresh()
Save entity and return the refreshed version from database
protected
saveAndRefresh(T $entity, callable $finder, string $entityType) : T
Ensures the returned entity has all database-generated values. Throws an exception if the entity cannot be found after save.
Parameters
- $entity : T
-
Entity to save
- $finder : callable
-
Callback to find entity by ID: function(int $id): ?T
- $entityType : string
-
Human-readable entity type for error messages
Tags
Return values
T —The refreshed entity from database