MySqlUserRepository
in package
implements
UserRepositoryInterface
MySQL implementation of UserRepositoryInterface.
Provides database access for user management operations. Handles authentication lookups and user CRUD.
Tags
Table of Contents
Interfaces
- UserRepositoryInterface
- Repository interface for User entity.
Properties
- $primaryKey : string
- $tableName : string
Methods
- activate() : bool
- Activate a user account.
- clearExpiredApiTokens() : int
- Clear expired API tokens.
- count() : int
- Count all users.
- countByRole() : array<string, int>
- Count users by role.
- deactivate() : bool
- Deactivate a user account.
- delete() : bool
- Delete a user by ID.
- deleteMultiple() : int
- Delete multiple users by IDs.
- emailExists() : bool
- Check if an email exists.
- exists() : bool
- Check if a user exists.
- find() : User|null
- Find a user by ID.
- findActive() : array<string|int, User>
- Find all active users.
- findAdmins() : array<string|int, User>
- Find all admin users.
- findByApiToken() : User|null
- Find a user by API token.
- findByEmail() : User|null
- Find a user by email.
- findByGoogleId() : User|null
- Find a user by Google ID.
- findByMicrosoftId() : User|null
- Find a user by Microsoft ID.
- findByPasswordResetToken() : User|null
- Find a user by password reset token.
- findByRememberToken() : User|null
- Find a user by remember-me token.
- findByUsername() : User|null
- Find a user by username.
- findByWordPressId() : User|null
- Find a user by WordPress ID.
- findGoogleUsers() : array<string|int, User>
- Find users linked to Google.
- findInactive() : array<string|int, User>
- Find all inactive users.
- findMicrosoftUsers() : array<string|int, User>
- Find users linked to Microsoft.
- findPaginated() : User[], total: int, page: int, per_page: int, total_pages: int}
- Get users with pagination.
- findRecentlyActive() : array<string|int, User>
- Find users who logged in recently.
- findRecentlyCreated() : array<string|int, User>
- Find users created recently.
- findWithExpiredApiTokens() : array<string|int, User>
- Find users with expired API tokens.
- findWordPressUsers() : array<string|int, User>
- Find users linked to WordPress.
- getBasicInfo() : array{id: int, username: string, email: string, is_active: bool, is_admin: bool}|null
- Get basic user info (minimal data for lists).
- getForSelect() : array<int, array{id: int, username: string, email: string}>
- Get users formatted for select dropdown options.
- getStatistics() : array{total: int, active: int, inactive: int, admins: int, wordpress_linked: int, google_linked: int, microsoft_linked: int}
- Get user statistics.
- linkGoogle() : bool
- Link user to Google account.
- linkMicrosoft() : bool
- Link user to Microsoft account.
- linkWordPress() : bool
- Link user to WordPress account.
- save() : int
- Save a user entity (create or update).
- search() : array<string|int, User>
- Search users by username or email.
- unlinkGoogle() : bool
- Unlink user from Google account.
- unlinkMicrosoft() : bool
- Unlink user from Microsoft account.
- unlinkWordPress() : bool
- Unlink user from WordPress account.
- updateApiToken() : bool
- Update the API token.
- updateLastLogin() : bool
- Update the last login timestamp.
- updatePassword() : bool
- Update the password hash.
- updatePasswordResetToken() : bool
- Update the password reset token.
- updateRememberToken() : bool
- Update the remember-me token.
- updateRole() : bool
- Update user role.
- usernameExists() : bool
- Check if a username exists.
- getNullableString() : string|null
- Get a nullable string from a mixed value.
- mapToEntity() : User
- Map a database row to a User entity.
- mapToRow() : array<string, scalar|null>
- Map a User entity to database row.
- parseDateTime() : DateTimeImmutable
- Parse a datetime string into DateTimeImmutable.
- parseNullableDateTime() : DateTimeImmutable|null
- Parse a nullable datetime string.
- query() : QueryBuilder
- Get a query builder for this repository's table.
Properties
$primaryKey
private
string
$primaryKey
= 'UsID'
Primary key column
$tableName
private
string
$tableName
= 'users'
Table name without prefix
Methods
activate()
Activate a user account.
public
activate(int $userId) : bool
Parameters
- $userId : int
-
User ID
Return values
bool —True if updated
clearExpiredApiTokens()
Clear expired API tokens.
public
clearExpiredApiTokens() : int
Return values
int —Number of cleared tokens
count()
Count all users.
public
count() : int
Return values
intcountByRole()
Count users by role.
public
countByRole() : array<string, int>
Return values
array<string, int> —Role => count
deactivate()
Deactivate a user account.
public
deactivate(int $userId) : bool
Parameters
- $userId : int
-
User ID
Return values
bool —True if updated
delete()
Delete a user by ID.
public
delete(int $id) : bool
Parameters
- $id : int
-
User ID
Return values
bool —True if deleted
deleteMultiple()
Delete multiple users by IDs.
public
deleteMultiple(array<string|int, int> $userIds) : int
Parameters
- $userIds : array<string|int, int>
-
Array of user IDs
Return values
int —Number of deleted users
emailExists()
Check if an email exists.
public
emailExists(string $email[, int|null $excludeId = null ]) : bool
Parameters
- $email : string
-
Email to check
- $excludeId : int|null = null
-
User ID to exclude (for updates)
Return values
boolexists()
Check if a user exists.
public
exists(int $id) : bool
Parameters
- $id : int
-
User ID
Return values
boolfind()
Find a user by ID.
public
find(int $id) : User|null
Parameters
- $id : int
-
User ID
Return values
User|nullfindActive()
Find all active users.
public
findActive() : array<string|int, User>
Return values
array<string|int, User>findAdmins()
Find all admin users.
public
findAdmins() : array<string|int, User>
Return values
array<string|int, User>findByApiToken()
Find a user by API token.
public
findByApiToken(string $token) : User|null
Parameters
- $token : string
-
The API token
Return values
User|nullfindByEmail()
Find a user by email.
public
findByEmail(string $email) : User|null
Parameters
- $email : string
-
The email address
Return values
User|nullfindByGoogleId()
Find a user by Google ID.
public
findByGoogleId(string $googleId) : User|null
Parameters
- $googleId : string
-
The Google user ID
Return values
User|nullfindByMicrosoftId()
Find a user by Microsoft ID.
public
findByMicrosoftId(string $microsoftId) : User|null
Parameters
- $microsoftId : string
-
The Microsoft user ID
Return values
User|nullfindByPasswordResetToken()
Find a user by password reset token.
public
findByPasswordResetToken(string $token) : User|null
Parameters
- $token : string
-
The password reset token (hashed)
Return values
User|nullfindByRememberToken()
Find a user by remember-me token.
public
findByRememberToken(string $token) : User|null
Parameters
- $token : string
-
The remember token
Return values
User|nullfindByUsername()
Find a user by username.
public
findByUsername(string $username) : User|null
Parameters
- $username : string
-
The username
Return values
User|nullfindByWordPressId()
Find a user by WordPress ID.
public
findByWordPressId(int $wordPressId) : User|null
Parameters
- $wordPressId : int
-
The WordPress user ID
Return values
User|nullfindGoogleUsers()
Find users linked to Google.
public
findGoogleUsers() : array<string|int, User>
Return values
array<string|int, User>findInactive()
Find all inactive users.
public
findInactive() : array<string|int, User>
Return values
array<string|int, User>findMicrosoftUsers()
Find users linked to Microsoft.
public
findMicrosoftUsers() : array<string|int, User>
Return values
array<string|int, User>findPaginated()
Get users with pagination.
public
findPaginated([int $page = 1 ][, int $perPage = 20 ][, string $orderBy = 'UsUsername' ][, string $direction = 'ASC' ]) : User[], total: int, page: int, per_page: int, total_pages: int}
Parameters
- $page : int = 1
-
Page number (1-based)
- $perPage : int = 20
-
Items per page
- $orderBy : string = 'UsUsername'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction
Return values
User[], total: int, page: int, per_page: int, total_pages: int}findRecentlyActive()
Find users who logged in recently.
public
findRecentlyActive([int $days = 30 ][, int $limit = 50 ]) : array<string|int, User>
Parameters
- $days : int = 30
-
Number of days to look back
- $limit : int = 50
-
Maximum results
Return values
array<string|int, User>findRecentlyCreated()
Find users created recently.
public
findRecentlyCreated([int $days = 30 ][, int $limit = 50 ]) : array<string|int, User>
Parameters
- $days : int = 30
-
Number of days to look back
- $limit : int = 50
-
Maximum results
Return values
array<string|int, User>findWithExpiredApiTokens()
Find users with expired API tokens.
public
findWithExpiredApiTokens() : array<string|int, User>
Return values
array<string|int, User>findWordPressUsers()
Find users linked to WordPress.
public
findWordPressUsers() : array<string|int, User>
Return values
array<string|int, User>getBasicInfo()
Get basic user info (minimal data for lists).
public
getBasicInfo(int $userId) : array{id: int, username: string, email: string, is_active: bool, is_admin: bool}|null
Parameters
- $userId : int
-
User ID
Return values
array{id: int, username: string, email: string, is_active: bool, is_admin: bool}|nullgetForSelect()
Get users formatted for select dropdown options.
public
getForSelect([int $maxNameLength = 40 ]) : array<int, array{id: int, username: string, email: string}>
Parameters
- $maxNameLength : int = 40
-
Maximum username length before truncation
Return values
array<int, array{id: int, username: string, email: string}>getStatistics()
Get user statistics.
public
getStatistics() : array{total: int, active: int, inactive: int, admins: int, wordpress_linked: int, google_linked: int, microsoft_linked: int}
Return values
array{total: int, active: int, inactive: int, admins: int, wordpress_linked: int, google_linked: int, microsoft_linked: int}linkGoogle()
Link user to Google account.
public
linkGoogle(int $userId, string $googleId) : bool
Parameters
- $userId : int
-
User ID
- $googleId : string
-
Google user ID
Return values
bool —True if updated
linkMicrosoft()
Link user to Microsoft account.
public
linkMicrosoft(int $userId, string $microsoftId) : bool
Parameters
- $userId : int
-
User ID
- $microsoftId : string
-
Microsoft user ID
Return values
bool —True if updated
linkWordPress()
Link user to WordPress account.
public
linkWordPress(int $userId, int $wordPressId) : bool
Parameters
- $userId : int
-
User ID
- $wordPressId : int
-
WordPress user ID
Return values
bool —True if updated
save()
Save a user entity (create or update).
public
save(User $entity) : int
Parameters
- $entity : User
-
The user to save
Return values
int —The user ID
search()
Search users by username or email.
public
search(string $query[, int $limit = 50 ]) : array<string|int, User>
Parameters
- $query : string
-
Search query
- $limit : int = 50
-
Maximum results
Return values
array<string|int, User>unlinkGoogle()
Unlink user from Google account.
public
unlinkGoogle(int $userId) : bool
Parameters
- $userId : int
-
User ID
Return values
bool —True if updated
unlinkMicrosoft()
Unlink user from Microsoft account.
public
unlinkMicrosoft(int $userId) : bool
Parameters
- $userId : int
-
User ID
Return values
bool —True if updated
unlinkWordPress()
Unlink user from WordPress account.
public
unlinkWordPress(int $userId) : bool
Parameters
- $userId : int
-
User ID
Return values
bool —True if updated
updateApiToken()
Update the API token.
public
updateApiToken(int $userId, string|null $token, DateTimeImmutable|null $expires) : bool
Parameters
- $userId : int
-
User ID
- $token : string|null
-
API token (null to clear)
- $expires : DateTimeImmutable|null
-
Token expiration
Return values
bool —True if updated
updateLastLogin()
Update the last login timestamp.
public
updateLastLogin(int $userId) : bool
Parameters
- $userId : int
-
User ID
Return values
bool —True if updated
updatePassword()
Update the password hash.
public
updatePassword(int $userId, string $passwordHash) : bool
Parameters
- $userId : int
-
User ID
- $passwordHash : string
-
New password hash
Return values
bool —True if updated
updatePasswordResetToken()
Update the password reset token.
public
updatePasswordResetToken(int $userId, string|null $token, DateTimeImmutable|null $expires) : bool
Parameters
- $userId : int
-
User ID
- $token : string|null
-
Password reset token (null to clear)
- $expires : DateTimeImmutable|null
-
Token expiration
Return values
bool —True if updated
updateRememberToken()
Update the remember-me token.
public
updateRememberToken(int $userId, string|null $token, DateTimeImmutable|null $expires) : bool
Parameters
- $userId : int
-
User ID
- $token : string|null
-
Remember token (null to clear)
- $expires : DateTimeImmutable|null
-
Token expiration
Return values
bool —True if updated
updateRole()
Update user role.
public
updateRole(int $userId, string $role) : bool
Parameters
- $userId : int
-
User ID
- $role : string
-
New role (user or admin)
Return values
bool —True if updated
usernameExists()
Check if a username exists.
public
usernameExists(string $username[, int|null $excludeId = null ]) : bool
Parameters
- $username : string
-
Username to check
- $excludeId : int|null = null
-
User ID to exclude (for updates)
Return values
boolgetNullableString()
Get a nullable string from a mixed value.
private
getNullableString(mixed $value) : string|null
Parameters
- $value : mixed
-
The value to convert
Return values
string|nullmapToEntity()
Map a database row to a User entity.
private
mapToEntity(array<string, mixed> $row) : User
Parameters
- $row : array<string, mixed>
-
Database row
Return values
UsermapToRow()
Map a User entity to database row.
private
mapToRow(User $entity) : array<string, scalar|null>
Parameters
- $entity : User
-
The user entity
Return values
array<string, scalar|null>parseDateTime()
Parse a datetime string into DateTimeImmutable.
private
parseDateTime(string|null $datetime) : DateTimeImmutable
Parameters
- $datetime : string|null
-
The datetime string
Return values
DateTimeImmutableparseNullableDateTime()
Parse a nullable datetime string.
private
parseNullableDateTime(string|null $datetime) : DateTimeImmutable|null
Parameters
- $datetime : string|null
-
The datetime string
Return values
DateTimeImmutable|nullquery()
Get a query builder for this repository's table.
private
query() : QueryBuilder