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