Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
82 / 82
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
EmailCollector
100.00% covered (success)
100.00%
82 / 82
100.00% covered (success)
100.00%
4 / 4
17
100.00% covered (success)
100.00%
1 / 1
 setMailer
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getActivities
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getContents
100.00% covered (success)
100.00%
65 / 65
100.00% covered (success)
100.00%
1 / 1
13
 showHeader
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Email Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Email\Debug;
11
12use Framework\Debug\Collector;
13use Framework\Debug\Debugger;
14use Framework\Email\Header;
15use Framework\Email\Mailer;
16
17/**
18 * Class EmailCollector.
19 *
20 * @package email
21 */
22class EmailCollector extends Collector
23{
24    protected Mailer $mailer;
25
26    public function setMailer(Mailer $mailer) : static
27    {
28        $this->mailer = $mailer;
29        return $this;
30    }
31
32    public function getActivities() : array
33    {
34        $activities = [];
35        foreach ($this->getData() as $index => $data) {
36            $activities[] = [
37                'collector' => $this->getName(),
38                'class' => static::class,
39                'description' => 'Send message ' . ($index + 1),
40                'start' => $data['start'],
41                'end' => $data['end'],
42            ];
43        }
44        return $activities;
45    }
46
47    public function getContents() : string
48    {
49        \ob_start();
50        if ( ! isset($this->mailer)) {
51            echo '<p>This collector has not been added to a Mailer instance.</p>';
52            return \ob_get_clean(); // @phpstan-ignore-line
53        }
54        echo $this->showHeader();
55        if ( ! $this->hasData()) {
56            echo '<p>No messages have been sent.</p>';
57            return \ob_get_clean(); // @phpstan-ignore-line
58        }
59        $count = \count($this->getData()); ?>
60        <p>Sent <?= $count ?> message<?= $count === 1 ? '' : 's' ?>:</p>
61        <?php
62        foreach ($this->getData() as $index => $data) : ?>
63            <h2>Message <?= $index + 1 ?></h2>
64            <p><strong>Status:</strong> <?= $data['code'] ?> <?=
65                $data['code'] === 250 ? 'OK' : ' Error' ?></p>
66            <p><strong>From:</strong> <?= \htmlentities($data['from']) ?></p>
67            <p>
68                <strong>Recipients:</strong> <?= \htmlentities(\implode(', ', $data['recipients'])) ?>
69            </p>
70            <p><strong>Size:</strong> <?= Debugger::convertSize($data['length']) ?></p>
71            <p>
72                <strong>Time Sending:</strong> <?= \round($data['end'] - $data['start'], 6) ?> seconds
73            </p>
74            <h3>Headers</h3>
75            <table>
76                <thead>
77                <tr>
78                    <th>Name</th>
79                    <th>Value</th>
80                </tr>
81                </thead>
82                <tbody>
83                <?php foreach ($data['headers'] as $name => $value): ?>
84                    <tr>
85                        <td><?= \htmlentities(Header::getName($name)) ?></td>
86                        <td><?= \htmlentities($value) ?></td>
87                    </tr>
88                <?php endforeach ?>
89                </tbody>
90            </table>
91            <?php
92            if (isset($data['html'])): ?>
93                <h3>HTML Message</h3>
94                <pre><code class="language-html"><?= \htmlentities($data['html']) ?></code></pre>
95            <?php
96            endif;
97            if (isset($data['plain'])): ?>
98                <h3>Plain Message</h3>
99                <pre><code class="language-none"><?= \htmlentities($data['plain']) ?></code></pre>
100            <?php
101            endif;
102            if ($data['attachments']): ?>
103                <h3>Attachments</h3>
104                <table>
105                    <thead>
106                    <tr>
107                        <th>File</th>
108                    </tr>
109                    </thead>
110                    <tbody>
111                    <?php foreach ($data['attachments'] as $attachment): ?>
112                        <tr>
113                            <td><?= \htmlentities(\realpath($attachment)); // @phpstan-ignore-line?></td>
114                        </tr>
115                    <?php endforeach ?>
116                    </tbody>
117                </table>
118            <?php
119            endif;
120            if ($data['inlineAttachments']): ?>
121                <h3>Inline Attachments</h3>
122                <table>
123                    <thead>
124                    <tr>
125                        <th>Content-ID</th>
126                        <th>File</th>
127                    </tr>
128                    </thead>
129                    <tbody>
130                    <?php foreach ($data['inlineAttachments'] as $cid => $filename): ?>
131                        <tr>
132                            <td><?= \htmlentities($cid) ?></td>
133                            <td><?= \htmlentities(\realpath($filename)); // @phpstan-ignore-line?></td>
134                        </tr>
135                    <?php endforeach ?>
136                    </tbody>
137                </table>
138            <?php
139            endif;
140        endforeach;
141        return \ob_get_clean(); // @phpstan-ignore-line
142    }
143
144    protected function showHeader() : string
145    {
146        \ob_start();
147        $configs = $this->mailer->getConfigs();
148        ?>
149        <p><strong>Host:</strong> <?= \htmlentities($configs['host']) ?></p>
150        <p><strong>Port:</strong> <?= \htmlentities((string) $configs['port']) ?></p>
151        <?php
152        return \ob_get_clean(); // @phpstan-ignore-line
153    }
154}