mail-account-admin/src/Repositories/AccountRepository.php

49 lines
1.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace MailAccountAdmin\Repositories;
2021-07-30 22:30:30 +02:00
use MailAccountAdmin\Exceptions\AccountNotFoundException;
use PDO;
class AccountRepository extends BaseRepository
{
public function fetchAccountList(string $filterByDomain = null): array
{
$queryWhere = '';
$queryParams = [];
if (!empty($filterByDomain)) {
$queryWhere = 'WHERE REGEXP_REPLACE(username, "^.*@", "") LIKE :domain';
$queryParams['domain'] = str_replace('*', '%', $filterByDomain);
}
$query = '
SELECT
mail_users.*,
REGEXP_REPLACE(username, "^.*@", "") AS domain,
COUNT(alias_id) AS alias_count
FROM mail_users
LEFT JOIN mail_aliases ON mail_users.user_id = mail_aliases.user_id
' . $queryWhere . '
GROUP BY username
ORDER BY domain, username
';
$statement = $this->pdo->prepare($query);
$statement->execute($queryParams);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
2021-07-30 22:30:30 +02:00
public function fetchAccountById(int $accountId): array
{
$statement = $this->pdo->prepare('SELECT * FROM mail_users WHERE user_id = :user_id LIMIT 1');
$statement->execute(['user_id' => $accountId]);
if ($statement->rowCount() < 1) {
throw new AccountNotFoundException("Account with user ID '$accountId' was not found.");
}
return $statement->fetch(PDO::FETCH_ASSOC);
}
}