1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Mailer\Debug; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Mailer\MessageInterface; |
8
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorTrait; |
9
|
|
|
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; |
10
|
|
|
|
11
|
|
|
final class MailerCollector implements SummaryCollectorInterface |
12
|
|
|
{ |
13
|
|
|
use CollectorTrait; |
14
|
|
|
|
15
|
|
|
private array $messages = []; |
16
|
|
|
|
17
|
|
|
public function collectMessage( |
18
|
|
|
MessageInterface $message, |
19
|
|
|
): void { |
20
|
|
|
$this->messages[] = $message; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function collectMessages( |
24
|
|
|
array $messages, |
25
|
|
|
): void { |
26
|
|
|
$this->messages = array_merge($this->messages, $messages); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getCollected(): array |
30
|
|
|
{ |
31
|
|
|
if (!$this->isActive()) { |
32
|
|
|
return []; |
33
|
|
|
} |
34
|
|
|
return [ |
35
|
|
|
'messages' => array_map(fn(MessageInterface $message) => [ |
36
|
|
|
'from' => (array) $message->getFrom(), |
37
|
|
|
'to' => (array) $message->getTo(), |
38
|
|
|
'subject' => $message->getSubject(), |
39
|
|
|
'textBody' => $message->getTextBody(), |
40
|
|
|
'htmlBody' => $message->getCharset() === 'quoted-printable' |
41
|
|
|
? quoted_printable_decode($message->getHtmlBody()) |
42
|
|
|
: $message->getHtmlBody(), |
43
|
|
|
'replyTo' => (array) $message->getReplyTo(), |
44
|
|
|
'cc' => (array) $message->getCc(), |
45
|
|
|
'bcc' => (array) $message->getBcc(), |
46
|
|
|
'charset' => $message->getCharset(), |
47
|
|
|
'date' => $message->getDate(), |
48
|
|
|
'raw' => (string) $message, |
49
|
|
|
], $this->messages), |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getSummary(): array |
54
|
|
|
{ |
55
|
|
|
if (!$this->isActive()) { |
56
|
|
|
return []; |
57
|
|
|
} |
58
|
|
|
return [ |
59
|
|
|
'mailer' => [ |
60
|
|
|
'total' => count($this->messages), |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function reset(): void |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->messages = []; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.