Passed
Pull Request — master (#72)
by Dmitriy
03:21
created

MailerCollector::collectMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
66
    {
67
        $this->messages = [];
68
    }
69
}
70