You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.5 KiB
PHP
92 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Ticket;
|
|
|
|
use App\Models\User;
|
|
use App\Services\Config;
|
|
|
|
/**
|
|
* Delivers post-commit ticket notifications with channel isolation.
|
|
*
|
|
* Each recipient and Telegram are independent attempts. A failed transport is
|
|
* reported and logged but never changes the already-committed ticket result or
|
|
* prevents the remaining channels from running.
|
|
*/
|
|
final class TicketNotificationDispatcher
|
|
{
|
|
public function __construct(
|
|
private ?TicketNotificationTransport $transport = null
|
|
) {
|
|
$this->transport ??= new TicketNotificationTransportAdapter();
|
|
}
|
|
|
|
public function dispatch(TicketEvent $event): TicketNotificationReport
|
|
{
|
|
$root = $event->result->root;
|
|
$owner = User::where('id', (int) $root->userid)->first();
|
|
$jobs = [];
|
|
$appName = (string) Config::get('appName');
|
|
$ticketUrl = rtrim((string) Config::get('baseUrl'), '/')
|
|
. '/user/ticket/' . (int) $root->id . '/view';
|
|
|
|
if ($event->kind === 'created' && $owner !== null && trim((string) $owner->email) !== '') {
|
|
$jobs[] = ['mail:owner', function () use ($owner, $root, $ticketUrl, $appName): void {
|
|
$this->transport->sendMail(
|
|
(string) $owner->email,
|
|
$appName . ' - 工单已创建',
|
|
'ticket/created.tpl',
|
|
['user' => $owner, 'ticket' => $root, 'ticket_url' => $ticketUrl],
|
|
'ticket_created'
|
|
);
|
|
}];
|
|
}
|
|
|
|
if (in_array($event->kind, ['created', 'user_reply'], true)) {
|
|
foreach (User::where('is_admin', 1)->get() as $admin) {
|
|
if (trim((string) $admin->email) === '') {
|
|
continue;
|
|
}
|
|
$jobs[] = ['mail:admin:' . (int) $admin->id, function () use ($admin, $root, $appName): void {
|
|
$this->transport->sendMail(
|
|
(string) $admin->email,
|
|
$appName . ' - ' . (string) $root->title,
|
|
'news/warn.tpl',
|
|
[
|
|
'user' => $admin,
|
|
'text' => '工单 #' . (int) $root->id . ' 有新的消息,请在网站管理页面查看。',
|
|
]
|
|
);
|
|
}];
|
|
}
|
|
$jobs[] = ['telegram', function () use ($event): void {
|
|
$this->transport->sendTelegram($event);
|
|
}];
|
|
}
|
|
|
|
if ($event->kind === 'admin_reply' && $owner !== null && trim((string) $owner->email) !== '') {
|
|
$jobs[] = ['mail:owner', function () use ($owner, $root, $ticketUrl, $appName): void {
|
|
$this->transport->sendMail(
|
|
(string) $owner->email,
|
|
$appName . ' - 工单收到回复',
|
|
'ticket/replied.tpl',
|
|
['user' => $owner, 'ticket' => $root, 'ticket_url' => $ticketUrl],
|
|
'ticket_replied'
|
|
);
|
|
}];
|
|
}
|
|
|
|
$succeeded = 0;
|
|
$errors = [];
|
|
foreach ($jobs as [$channel, $job]) {
|
|
try {
|
|
$job();
|
|
$succeeded += 1;
|
|
} catch (\Throwable $exception) {
|
|
$errors[] = ['channel' => $channel, 'error' => $exception->getMessage()];
|
|
error_log('Ticket notification failed [' . $channel . ']: ' . $exception->getMessage());
|
|
}
|
|
}
|
|
return new TicketNotificationReport(count($jobs), $succeeded, $errors);
|
|
}
|
|
}
|