Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
Mail
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
10 / 10
25
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getMailer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEmails
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setEmails
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addEmails
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 setOptions
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addOption
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addOptions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 writeLog
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
12
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Log\Writer;
16
17use Pop\Mail\Mailer;
18use Pop\Mail\Message;
19use Pop\Mail\Queue;
20
21/**
22 * Mail log writer class
23 *
24 * @category   Pop
25 * @package    Pop\Log
26 * @author     Nick Sagona, III <dev@noladev.com>
27 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
28 * @license    https://www.popphp.org/license     New BSD License
29 * @version    5.0.0
30 */
31class Mail extends AbstractWriter
32{
33
34    /**
35     * Mailer object
36     * @var ?Mailer
37     */
38    protected ?Mailer $mailer = null;
39
40    /**
41     * Emails to which to send the log messages
42     * @var array
43     */
44    protected array $emails = [];
45
46    /**
47     * Array of mail-specific options, i.e. subject, headers, etc.
48     * @var array
49     */
50    protected array $options = [];
51
52    /**
53     * Constructor
54     *
55     * Instantiate the Mail writer object
56     *
57     * @param  Mailer $mailer
58     * @param  mixed  $emails
59     * @param  array  $options
60     */
61    public function __construct(Mailer $mailer, mixed $emails, array $options = [])
62    {
63        $this->mailer = $mailer;
64        $this->setEmails($emails);
65        $this->setOptions($options);
66    }
67
68    /**
69     * Get mailer
70     * @return Mailer
71     */
72    public function getMailer(): Mailer
73    {
74        return $this->mailer;
75    }
76
77    /**
78     * Get emails
79     * @return array
80     */
81    public function getEmails(): array
82    {
83        return $this->emails;
84    }
85
86    /**
87     * Get options
88     * @return array
89     */
90    public function getOptions(): array
91    {
92        return $this->options;
93    }
94
95    /**
96     * Set emails
97     *
98     * @param  mixed $emails
99     * @return Mail
100     */
101    public function setEmails(mixed $emails): Mail
102    {
103        $this->emails = [];
104        $this->addEmails($emails);
105        return $this;
106    }
107
108    /**
109     * Add emails
110     *
111     * @param  mixed $emails
112     * @return Mail
113     */
114    public function addEmails(mixed $emails): Mail
115    {
116        if (is_array($emails)) {
117            foreach ($emails as $email) {
118                $this->emails[] = ['email' => $email];
119            }
120        } else if (is_string($emails)) {
121            $this->emails[] = ['email' => $emails];
122        }
123        return $this;
124    }
125
126    /**
127     * Set options
128     *
129     * @param  array $options
130     * @return Mail
131     */
132    public function setOptions(array $options): Mail
133    {
134        $this->options = $options;
135        return $this;
136    }
137
138    /**
139     * Add option
140     *
141     * @param  mixed $option
142     * @param  mixed $value
143     * @return Mail
144     */
145    public function addOption(mixed $option, mixed $value): Mail
146    {
147        $this->options[$option] = $value;
148        return $this;
149    }
150
151    /**
152     * Add options
153     *
154     * @param  array $options
155     * @return Mail
156     */
157    public function addOptions(array $options): Mail
158    {
159        foreach ($options as $option => $value) {
160            $this->addOption($option, $value);
161        }
162        return $this;
163    }
164
165    /**
166     * Write to the log
167     *
168     * @param  string $level
169     * @param  string $message
170     * @param  array  $context
171     * @return Mail
172     */
173    public function writeLog(string $level, string $message, array $context = []): Mail
174    {
175        if ($this->isWithinLogLimit($level)) {
176            $subject = (isset($this->options['subject'])) ?
177                $this->options['subject'] : 'Log Entry:';
178
179            $subject .= ' ' . $this->sanitize($context['name']) . ' (' . $level . ')';
180
181            $queue       = new Queue($this->emails);
182            $mailMessage = new Message($subject);
183
184            if (isset($this->options['headers'])) {
185                foreach ($this->options['headers'] as $header => $value) {
186                    switch (strtolower($header)) {
187                        case 'cc':
188                            $mailMessage->setCc($value);
189                            break;
190                        case 'bcc':
191                            $mailMessage->setBcc($value);
192                            break;
193                        case 'from':
194                            $mailMessage->setFrom($value);
195                            break;
196                        case 'reply-to':
197                            $mailMessage->setReplyTo($value);
198                            break;
199                        case 'sender':
200                            $mailMessage->setSender($value);
201                            break;
202                        case 'return-path':
203                            $mailMessage->setReturnPath($value);
204                            break;
205                        default:
206                            $mailMessage->addHeader($header, (string)$value);
207                    }
208                }
209            }
210
211            $mailMessage->setBody(
212                $this->sanitize($context['timestamp']) . "\t" . $level . "\t" . $this->sanitize($context['name']) . "\t" .
213                $this->sanitize($message) . "\t" . $this->sanitize($this->getContext($context)) . PHP_EOL
214            );
215
216            $queue->addMessage($mailMessage);
217            $this->mailer->sendFromQueue($queue);
218        }
219
220        return $this;
221    }
222
223}