Passed
Pull Request — master (#2)
by
unknown
15:51
created

SwiftMailerHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 6
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Monolog package.
5
 *
6
 * (c) Jordi Boggiano <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Monolog\Handler;
13
14
use Monolog\Logger;
15
use Monolog\Formatter\LineFormatter;
16
use Swift;
17
18
/**
19
 * SwiftMailerHandler uses Swift_Mailer to send the emails
20
 *
21
 * @author Gyula Sallai
22
 */
23
class SwiftMailerHandler extends MailHandler
24
{
25
    protected $mailer;
26
    private $messageTemplate;
27
28
    /**
29
     * @param \Swift_Mailer           $mailer  The mailer to use
30
     * @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced
31
     * @param int                     $level   The minimum logging level at which this handler will be triggered
32
     * @param Boolean                 $bubble  Whether the messages that are handled can bubble up the stack or not
33
     */
34
    public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true)
35
    {
36
        parent::__construct($level, $bubble);
37
38
        $this->mailer = $mailer;
39
        $this->messageTemplate = $message;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function send($content, array $records)
46
    {
47
        $this->mailer->send($this->buildMessage($content, $records));
48
    }
49
50
    /**
51
     * Creates instance of Swift_Message to be sent
52
     *
53
     * @param  string         $content formatted email body to be sent
54
     * @param  array          $records Log records that formed the content
55
     * @return \Swift_Message
56
     */
57
    protected function buildMessage($content, array $records)
58
    {
59
        $message = null;
60
        if ($this->messageTemplate instanceof \Swift_Message) {
61
            $message = clone $this->messageTemplate;
62
            $message->generateId();
63
        } elseif (is_callable($this->messageTemplate)) {
64
            $message = call_user_func($this->messageTemplate, $content, $records);
65
        }
66
67
        if (!$message instanceof \Swift_Message) {
68
            throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
69
        }
70
71
        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...
72
            $subjectFormatter = new LineFormatter($message->getSubject());
73
            $message->setSubject($subjectFormatter->format($this->getHighestRecord($records)));
74
        }
75
76
        $message->setBody($content);
77
        if (version_compare(Swift::VERSION, '6.0.0', '>=')) {
78
            $message->setDate(new \DateTimeImmutable());
0 ignored issues
show
Bug introduced by
new DateTimeImmutable() of type DateTimeImmutable is incompatible with the type integer expected by parameter $date of Swift_Mime_SimpleMessage::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            $message->setDate(/** @scrutinizer ignore-type */ new \DateTimeImmutable());
Loading history...
79
        } else {
80
            $message->setDate(time());
81
        }
82
83
        return $message;
84
    }
85
86
    /**
87
     * BC getter, to be removed in 2.0
88
     */
89
    public function __get($name)
90
    {
91
        if ($name === 'message') {
92
            trigger_error('SwiftMailerHandler->message is deprecated, use ->buildMessage() instead to retrieve the message', E_USER_DEPRECATED);
93
94
            return $this->buildMessage(null, array());
95
        }
96
97
        throw new \InvalidArgumentException('Invalid property '.$name);
98
    }
99
}
100