Completed
Pull Request — master (#35)
by Chris
09:39
created

LoggingCourier::mapAddresses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Courier;
6
7
use Courier\Exceptions\TransmissionException;
8
use Courier\Exceptions\UnsupportedContentException;
9
use PhpEmail\Address;
10
use PhpEmail\Attachment;
11
use PhpEmail\Content\Contracts\SimpleContent;
12
use PhpEmail\Content\Contracts\TemplatedContent;
13
use PhpEmail\Email;
14
use Psr\Log\LoggerInterface;
15
16
/**
17
 * A Courier implementation the writes the information to a logger.
18
 *
19
 * This implementation is not designed to be used in a production system, but it can act as a drop-in testing implementation,
20
 * allowing developers to build content and log it to their local systems, without delivering emails. This can be
21
 * helpful in cases where the email might contain important information, like a generated password reset token.
22
 */
23
class LoggingCourier implements Courier
24
{
25
    /**
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
30
    public function __construct(LoggerInterface $logger)
31
    {
32
        $this->logger = $logger;
33
    }
34
35
    /**
36
     * @param Email $email
37
     *
38
     * @throws TransmissionException
39
     * @throws UnsupportedContentException
40
     *
41
     * @return void
42
     */
43
    public function deliver(Email $email): void
44
    {
45
        $this->logger->debug('Delivered email');
46
        $this->logger->debug("Subject: {$email->getSubject()}");
47
        $this->logger->debug("From: {$email->getFrom()->toRfc2822()}");
48
        $this->logger->debug("Reply To: {$this->mapAddresses($email->getReplyTos())}");
49
        $this->logger->debug("To: {$this->mapAddresses($email->getToRecipients())}");
50
        $this->logger->debug("CC: {$this->mapAddresses($email->getCcRecipients())}");
51
        $this->logger->debug("BCC: {$this->mapAddresses($email->getBccRecipients())}");
52
53
        $this->logHeaders($email);
54
        $this->logAttachments($email);
55
        $this->logContent($email);
56
    }
57
58
    private function mapAddresses(array $addresses): string
59
    {
60
        return implode(', ', array_map(function (Address $address) {
61
            return $address->toRfc2822();
62
        }, $addresses));
63
    }
64
65
    private function logHeaders(Email $email): void
66
    {
67
        foreach ($email->getHeaders() as $header) {
68
            $this->logger->debug("{$header->getField()}: {$header->getValue()}");
69
        }
70
    }
71
72
    private function logAttachments(Email $email): void
73
    {
74
        $attachmentNames = implode(', ', array_map(function (Attachment $attachment) {
75
            return $attachment->getName();
76
        }, $email->getAttachments()));
77
78
        $embeddedIds = implode(', ', array_map(function (Attachment $attachment) {
79
            return $attachment->getContentId() ?? 'NA';
80
        }, $email->getAttachments()));
81
82
        $this->logger->debug("Attaching: $attachmentNames");
83
        $this->logger->debug("Embedding IDs: $embeddedIds");
84
    }
85
86
    private function logContent(Email $email): void
87
    {
88
        $content = $email->getContent();
89
        if ($content instanceof TemplatedContent) {
90
            $this->logger->debug("Template ID: {$content->getTemplateId()}");
91
            $this->logger->debug("Template Data:\n" . json_encode($content->getTemplateData(), JSON_PRETTY_PRINT));
92
        }
93
94
        if ($content instanceof SimpleContent) {
95
            $this->logger->debug("HTML:\n");
96
            $this->logger->debug($content->getHtml()->getBody());
97
            $this->logger->debug("Text:\n");
98
            $this->logger->debug($content->getText()->getBody());
99
        }
100
    }
101
}
102