-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathCommandExecuted.php
More file actions
54 lines (46 loc) · 2.21 KB
/
CommandExecuted.php
File metadata and controls
54 lines (46 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php declare(strict_types=1);
namespace App\Notifications;
use Ibexa\Contracts\Notifications\SystemNotification\SystemMessage;
use Ibexa\Contracts\Notifications\SystemNotification\SystemNotificationInterface;
use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipientInterface;
use Symfony\Bridge\Twig\Mime\NotificationEmail;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
use Throwable;
class CommandExecuted extends Notification implements SystemNotificationInterface, EmailNotificationInterface
{
/** @param array<int, Throwable> $exceptions */
public function __construct(
private readonly Command $command,
private readonly int $exitCode,
private readonly array $exceptions
) {
parent::__construct((Command::SUCCESS === $this->exitCode ? '✔' : '✖') . $this->command->getName());
$this->importance(Command::SUCCESS === $this->exitCode ? Notification::IMPORTANCE_LOW : Notification::IMPORTANCE_HIGH);
}
public function asEmailMessage(EmailRecipientInterface $recipient, ?string $transport = null): ?EmailMessage
{
$body = '';
foreach ($this->exceptions as $exception) {
$body .= $exception->getMessage() . '<br>';
}
$email = NotificationEmail::asPublicEmail()
->to($recipient->getEmail())
->subject($this->getSubject())
->html($body);
return new EmailMessage($email);
}
public function asSystemNotification(UserRecipientInterface $recipient, ?string $transport = null): ?SystemMessage
{
$message = new SystemMessage($recipient->getUser());
$message->setContext([
'icon' => Command::SUCCESS === $this->exitCode ? 'check-circle' : 'discard-circle',
'subject' => $this->command->getName(),
'content' => Command::SUCCESS === $this->exitCode ? 'No error' : count($this->exceptions) . ' error' . (count($this->exceptions) > 1 ? 's' : ''),
]);
return $message;
}
}