Neuron-PHP

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

Methods

__construct()

Constructor

public __construct(SettingManager $settings) : mixed
Parameters
$settings : SettingManager

Settings manager with database configuration

Tags
throws
Exception

if database configuration is missing or adapter is unsupported

__sleep()

Handle serialization for PHPUnit process isolation

public __sleep() : array<string|int, mixed>
Return values
array<string|int, mixed>

all()

Get all users

public all() : array<string|int, mixed>
Return values
array<string|int, mixed>

findByRememberToken()

Find user by remember token

public findByRememberToken(string $token) : User|null
Parameters
$token : string
Return values
User|null

findByUsername()

Find user by username

public findByUsername(string $username) : User|null
Parameters
$username : string
Return values
User|null

incrementFailedLoginAttempts()

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

createEntity()

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:

  1. Setting timestamps if not already set
  2. Saving the entity
  3. 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
template
throws
RepositoryException

If entity cannot be saved or found after save

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
template
throws
RepositoryException

If entity cannot be found after save

Return values
T

The refreshed entity from database


        
On this page

Search results