1
0
Files
breadcrumb-the-shire/lib/Repository/Mail/MailLogRepository.php

120 lines
3.9 KiB
PHP
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
2026-02-11 19:28:12 +01:00
namespace MintyPHP\Repository\Mail;
2026-02-04 23:31:53 +01:00
2026-03-04 15:56:58 +01:00
use MintyPHP\Domain\Taxonomy\MailLogStatus;
2026-02-04 23:31:53 +01:00
use MintyPHP\DB;
2026-02-11 19:28:12 +01:00
use MintyPHP\Repository\Support\RepoQuery;
2026-02-04 23:31:53 +01:00
class MailLogRepository
{
2026-02-23 12:58:19 +01:00
public function create(array $data): ?int
2026-02-04 23:31:53 +01:00
{
$id = DB::insert(
'insert into mail_log (to_email, subject, template, status, created_at) values (?,?,?,?,NOW())',
$data['to_email'],
$data['subject'],
$data['template'] ?? null,
2026-03-04 15:56:58 +01:00
$data['status'] ?? MailLogStatus::Queued->value
2026-02-04 23:31:53 +01:00
);
return $id ? (int) $id : null;
}
2026-02-23 12:58:19 +01:00
public function markSent(int $id, ?string $providerMessageId = null): bool
2026-02-04 23:31:53 +01:00
{
$result = DB::update(
'update mail_log set status = ?, sent_at = NOW(), provider_message_id = ?, error_message = NULL where id = ?',
2026-03-04 15:56:58 +01:00
MailLogStatus::Sent->value,
2026-02-04 23:31:53 +01:00
$providerMessageId,
(string) $id
);
return $result !== false;
}
2026-02-23 12:58:19 +01:00
public function markFailed(int $id, string $errorMessage): bool
2026-02-04 23:31:53 +01:00
{
$result = DB::update(
'update mail_log set status = ?, error_message = ? where id = ?',
2026-03-04 15:56:58 +01:00
MailLogStatus::Failed->value,
2026-02-04 23:31:53 +01:00
$errorMessage,
(string) $id
);
return $result !== false;
}
2026-02-23 12:58:19 +01:00
public function listPaged(array $options): array
2026-02-04 23:31:53 +01:00
{
$search = trim((string) ($options['search'] ?? ''));
2026-03-04 15:56:58 +01:00
$status = MailLogStatus::tryNormalize((string) ($options['status'] ?? ''))?->value ?? '';
2026-02-04 23:31:53 +01:00
$createdFrom = trim((string) ($options['created_from'] ?? ''));
$createdTo = trim((string) ($options['created_to'] ?? ''));
$allowedOrder = ['id', 'created_at', 'sent_at', 'to_email', 'subject', 'status'];
2026-02-11 19:28:12 +01:00
[$limit, $offset] = RepoQuery::sanitizeLimitOffset($options);
[$order, $dir] = RepoQuery::sanitizeOrder($options, $allowedOrder, 'created_at', 'desc');
2026-02-04 23:31:53 +01:00
$where = [];
$params = [];
2026-02-11 19:28:12 +01:00
RepoQuery::addLikeFilter($where, $params, ['to_email', 'subject', 'template'], $search);
RepoQuery::addEqualsFilter($where, $params, $status, 'status = ?');
2026-02-04 23:31:53 +01:00
if ($createdFrom !== '') {
$where[] = 'created_at >= ?';
$params[] = $createdFrom . ' 00:00:00';
}
if ($createdTo !== '') {
$where[] = 'created_at <= ?';
$params[] = $createdTo . ' 23:59:59';
}
$whereSql = $where ? (' where ' . implode(' and ', $where)) : '';
$count = DB::selectValue('select count(*) from mail_log' . $whereSql, ...$params);
$total = $count ? (int) $count : 0;
$query = 'select id, to_email, subject, template, status, created_at, sent_at, error_message, provider_message_id from mail_log' .
$whereSql .
sprintf(' order by `%s` %s limit ? offset ?', $order, $dir);
$queryParams = array_merge($params, [(string) $limit, (string) $offset]);
$rows = call_user_func_array(['MintyPHP\\DB', 'select'], array_merge([$query], $queryParams));
return [
'total' => $total,
2026-02-23 12:58:19 +01:00
'rows' => $this->unwrapList($rows),
2026-02-04 23:31:53 +01:00
];
}
2026-02-23 12:58:19 +01:00
public function find(int $id): ?array
2026-02-04 23:31:53 +01:00
{
$row = DB::selectOne(
'select id, to_email, subject, template, status, created_at, sent_at, error_message, provider_message_id from mail_log where id = ? limit 1',
(string) $id
);
2026-02-23 12:58:19 +01:00
return $this->unwrap($row);
}
private function unwrap(?array $row): ?array
{
if (!$row) {
return null;
}
return $row['mail_log'] ?? null;
}
private function unwrapList(mixed $rows): array
{
if (!is_array($rows)) {
return [];
}
$list = [];
foreach ($rows as $row) {
$mailLog = $row['mail_log'] ?? null;
if (is_array($mailLog)) {
$list[] = $mailLog;
}
}
return $list;
2026-02-04 23:31:53 +01:00
}
}