PHPMailerHandler1   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 64
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 4 1
A __construct() 0 4 1
A buildMessage() 0 20 3
1
<?php
2
3
namespace MonologPHPMailer;
4
5
use Monolog\Formatter\FormatterInterface;
6
use Monolog\Formatter\LineFormatter;
7
use Monolog\Handler\MailHandler;
8
use Monolog\Logger;
9
use PHPMailer\PHPMailer\PHPMailer;
10
11
/**
12
 * PHPMailer handler for Monolog 1.x.
13
 *
14
 * It uses [PHPMailer](https://github.com/PHPMailer/PHPMailer/) to send emails.
15
 *
16
 * @since 1.0.0
17
 *
18
 * @author Filip Š <[email protected]>
19
 *
20
 * @license MIT
21
 *
22
 * @package MonologPHPMailer
23
 */
24
class PHPMailerHandler1 extends MailHandler
25
{
26
    /**
27
     * A PHPMailer instance.
28
     *
29
     * @var PHPMailer $mailer
30
     */
31
    protected $mailer;
32
33
    /**
34
     * Constructs the PHPMailer handler.
35
     *
36
     * @param PHPMailer  $mailer A PHPMailer instance to use.
37
     * @param int|string $level  The minimum logging level at which this handler will be triggered.
38
     * @param bool       $bubble Whether the messages that are handled can bubble up the stack or not.
39
     */
40 3
    public function __construct(PHPMailer $mailer, $level = Logger::ERROR, $bubble = true)
41
    {
42 3
        parent::__construct($level, $bubble);
43 3
        $this->mailer = $mailer;
44 3
    }
45
46
    /**
47
     * Sends a mail with the given content.
48
     *
49
     * @param string $content Formatted email body to be sent.
50
     * @param array  $records The array of log records that formed this content.
51
     *
52
     * @return void
53
     */
54 1
    protected function send($content, array $records)
55
    {
56 1
        $mailer = $this->buildMessage($content, $records);
57 1
        $mailer->send();
58 1
    }
59
60
    /**
61
     * Builds a message to be sent.
62
     *
63
     * @param string    $content Formatted email body to be sent.
64
     * @param array     $records The array of log records that formed this content.
65
     *
66
     * @return PHPMailer Builded message.
67
     */
68 2
    public function buildMessage($content, array $records)
69
    {
70 2
        $mailer = clone $this->mailer;
71
72 2
        if (substr($content, 0, 1) == '<') {
73 1
            $mailer->isHTML(true);
74
        }
75
76
        // @codingStandardsIgnoreStart
77 2
        if ($records) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $records of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78 2
            $subjectFormatter = new LineFormatter($mailer->Subject);
79 2
            $mailer->Subject = $subjectFormatter->format($this->getHighestRecord($records));
80
        }
81
        // @codingStandardsIgnoreEnd
82
83
        // @codingStandardsIgnoreStart
84 2
        $mailer->Body = $content;
85
        // @codingStandardsIgnoreEnd
86
87 2
        return $mailer;
88
    }
89
}
90