Passed
Pull Request — master (#72)
by Dmitriy
02:46
created

MailerCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 57
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A collectMessages() 0 4 1
A getSummary() 0 8 2
A getCollected() 0 21 3
A collectMessage() 0 4 1
A reset() 0 3 1
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