Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
41 / 41 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
|
100.00% |
41 / 41 |
|
100.00% |
2 / 2 |
16 | |
100.00% |
1 / 1 |
|
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
writeLog | |
100.00% |
34 / 34 |
|
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-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Log\Writer; |
15 | |
16 | use Pop\Mail\Mailer; |
17 | use Pop\Mail\Message; |
18 | use 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-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
27 | * @license http://www.popphp.org/license New BSD License |
28 | * @version 3.3.0 |
29 | */ |
30 | class Mail extends AbstractWriter |
31 | { |
32 | |
33 | /** |
34 | * Mailer object |
35 | * @var Mailer |
36 | */ |
37 | protected $mailer = null; |
38 | |
39 | /** |
40 | * Emails to which to send the log messages |
41 | * @var array |
42 | */ |
43 | protected $emails = []; |
44 | |
45 | /** |
46 | * Array of mail-specific options, i.e. subject, headers, etc. |
47 | * @var array |
48 | */ |
49 | protected $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, $emails, array $options = []) |
61 | { |
62 | $this->mailer = $mailer; |
63 | $this->options = $options; |
64 | |
65 | if (is_array($emails)) { |
66 | foreach ($emails as $email) { |
67 | $this->emails[] = ['email' => $email]; |
68 | } |
69 | } else if (is_string($emails)) { |
70 | $this->emails[] = ['email' => $emails]; |
71 | } |
72 | } |
73 | |
74 | /** |
75 | * Write to the log |
76 | * |
77 | * @param mixed $level |
78 | * @param string $message |
79 | * @param array $context |
80 | * @return Mail |
81 | */ |
82 | public function writeLog($level, $message, array $context = []) |
83 | { |
84 | if ($this->isWithinLogLimit($level)) { |
85 | $subject = (isset($this->options['subject'])) ? |
86 | $this->options['subject'] : 'Log Entry:'; |
87 | |
88 | $subject .= ' ' . $context['name'] . ' (' . $level . ')'; |
89 | |
90 | $queue = new Queue($this->emails); |
91 | $mailMessage = new Message($subject); |
92 | |
93 | if (isset($this->options['headers'])) { |
94 | foreach ($this->options['headers'] as $header => $value) { |
95 | switch (strtolower($header)) { |
96 | case 'cc': |
97 | $mailMessage->setCc($value); |
98 | break; |
99 | case 'bcc': |
100 | $mailMessage->setBcc($value); |
101 | break; |
102 | case 'from': |
103 | $mailMessage->setFrom($value); |
104 | break; |
105 | case 'reply-to': |
106 | $mailMessage->setReplyTo($value); |
107 | break; |
108 | case 'sender': |
109 | $mailMessage->setSender($value); |
110 | break; |
111 | case 'return-path': |
112 | $mailMessage->setReturnPath($value); |
113 | break; |
114 | default: |
115 | $mailMessage->addHeader($header, $value); |
116 | } |
117 | } |
118 | } |
119 | |
120 | $mailMessage->setBody( |
121 | $context['timestamp'] . "\t" . $level . "\t" . $context['name'] . "\t" . |
122 | $message . "\t" . $this->getContext($context) . PHP_EOL |
123 | ); |
124 | |
125 | $queue->addMessage($mailMessage); |
126 | $this->mailer->sendFromQueue($queue); |
127 | } |
128 | |
129 | return $this; |
130 | } |
131 | |
132 | } |