/
Notification.php
215 lines (178 loc) · 5.17 KB
/
Notification.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Notifier\Notification;
use Psr\Log\LogLevel;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\Notifier\Recipient\RecipientInterface;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Notification
{
private const LEVELS = [
LogLevel::DEBUG => 100,
LogLevel::INFO => 200,
LogLevel::NOTICE => 250,
LogLevel::WARNING => 300,
LogLevel::ERROR => 400,
LogLevel::CRITICAL => 500,
LogLevel::ALERT => 550,
LogLevel::EMERGENCY => 600,
];
public const IMPORTANCE_URGENT = 'urgent';
public const IMPORTANCE_HIGH = 'high';
public const IMPORTANCE_MEDIUM = 'medium';
public const IMPORTANCE_LOW = 'low';
private array $channels = [];
private string $subject = '';
private string $content = '';
private string $emoji = '';
private ?FlattenException $exception = null;
private string $exceptionAsString = '';
private string $importance = self::IMPORTANCE_HIGH;
/**
* @param list<string> $channels
*/
public function __construct(string $subject = '', array $channels = [])
{
$this->subject = $subject;
$this->channels = $channels;
}
/**
* @param list<string> $channels
*/
public static function fromThrowable(\Throwable $exception, array $channels = []): self
{
return (new self('', $channels))->exception($exception);
}
/**
* @return $this
*/
public function subject(string $subject): static
{
$this->subject = $subject;
return $this;
}
public function getSubject(): string
{
return $this->subject;
}
/**
* @return $this
*/
public function content(string $content): static
{
$this->content = $content;
return $this;
}
public function getContent(): string
{
return $this->content;
}
/**
* @return $this
*/
public function importance(string $importance): static
{
$this->importance = $importance;
return $this;
}
public function getImportance(): string
{
return $this->importance;
}
/**
* @param string $level A PSR Logger log level name
*
* @return $this
*/
public function importanceFromLogLevelName(string $level): static
{
$level = self::LEVELS[strtolower($level)];
$this->importance = $level >= 500 ? self::IMPORTANCE_URGENT : ($level >= 400 ? self::IMPORTANCE_HIGH : self::IMPORTANCE_LOW);
return $this;
}
/**
* @return $this
*/
public function emoji(string $emoji): static
{
$this->emoji = $emoji;
return $this;
}
public function getEmoji(): string
{
return $this->emoji ?: $this->getDefaultEmoji();
}
/**
* @return $this
*/
public function exception(\Throwable $exception): static
{
$parts = explode('\\', $exception::class);
$this->subject = sprintf('%s: %s', array_pop($parts), $exception->getMessage());
if (class_exists(FlattenException::class)) {
$this->exception = $exception instanceof FlattenException ? $exception : FlattenException::createFromThrowable($exception);
}
$this->exceptionAsString = $this->computeExceptionAsString($exception);
return $this;
}
public function getException(): ?FlattenException
{
return $this->exception;
}
public function getExceptionAsString(): string
{
return $this->exceptionAsString;
}
/**
* @param list<string> $channels
*
* @return $this
*/
public function channels(array $channels): static
{
$this->channels = $channels;
return $this;
}
/**
* @return list<string>
*/
public function getChannels(RecipientInterface $recipient): array
{
return $this->channels;
}
protected function getDefaultEmoji(): string
{
if (!$this->exceptionAsString) {
return '';
}
return match ($this->importance) {
self::IMPORTANCE_URGENT => '🌩️',
self::IMPORTANCE_HIGH => '🌧️',
self::IMPORTANCE_MEDIUM => '🌦️',
default => '⛅',
};
}
private function computeExceptionAsString(\Throwable $exception): string
{
if (class_exists(FlattenException::class)) {
$exception = $exception instanceof FlattenException ? $exception : FlattenException::createFromThrowable($exception);
return $exception->getAsString();
}
$message = $exception::class;
if ('' !== $exception->getMessage()) {
$message .= ': '.$exception->getMessage();
}
$message .= ' in '.$exception->getFile().':'.$exception->getLine()."\n";
$message .= "Stack trace:\n".$exception->getTraceAsString()."\n\n";
return rtrim($message);
}
}