Passed
Push — fix-php-74 ( f6eb65...a1ad6b )
by Alexander
38:08 queued 12:19
created

EmailTarget::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\log;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\di\Instance;
13
use yii\mail\MailerInterface;
14
15
/**
16
 * EmailTarget sends selected log messages to the specified email addresses.
17
 *
18
 * You may configure the email to be sent by setting the [[message]] property, through which
19
 * you can set the target email addresses, subject, etc.:
20
 *
21
 * ```php
22
 * 'components' => [
23
 *     'log' => [
24
 *          'targets' => [
25
 *              [
26
 *                  'class' => 'yii\log\EmailTarget',
27
 *                  'mailer' => 'mailer',
28
 *                  'levels' => ['error', 'warning'],
29
 *                  'message' => [
30
 *                      'from' => ['[email protected]'],
31
 *                      'to' => ['[email protected]', '[email protected]'],
32
 *                      'subject' => 'Log message',
33
 *                  ],
34
 *              ],
35
 *          ],
36
 *     ],
37
 * ],
38
 * ```
39
 *
40
 * In the above `mailer` is ID of the component that sends email and should be already configured.
41
 *
42
 * @author Qiang Xue <[email protected]>
43
 * @since 2.0
44
 */
45
class EmailTarget extends Target
46
{
47
    /**
48
     * @var array the configuration array for creating a [[\yii\mail\MessageInterface|message]] object.
49
     * Note that the "to" option must be set, which specifies the destination email address(es).
50
     */
51
    public $message = [];
52
    /**
53
     * @var MailerInterface|array|string the mailer object or the application component ID of the mailer object.
54
     * After the EmailTarget object is created, if you want to change this property, you should only assign it
55
     * with a mailer object.
56
     * Starting from version 2.0.2, this can also be a configuration array for creating the object.
57
     */
58
    public $mailer = 'mailer';
59
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 2
    public function init()
65
    {
66 2
        parent::init();
67 2
        if (empty($this->message['to'])) {
68 1
            throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.');
69
        }
70 1
        $this->mailer = Instance::ensure($this->mailer, 'yii\mail\MailerInterface');
71 1
    }
72
73
    /**
74
     * Sends log messages to specified email addresses.
75
     * Starting from version 2.0.14, this method throws LogRuntimeException in case the log can not be exported.
76
     * @throws LogRuntimeException
77
     */
78 3
    public function export()
79
    {
80
        // moved initialization of subject here because of the following issue
81
        // https://github.com/yiisoft/yii2/issues/1446
82 3
        if (empty($this->message['subject'])) {
83 2
            $this->message['subject'] = 'Application Log';
84
        }
85 3
        $messages = array_map([$this, 'formatMessage'], $this->messages);
86 3
        $body = wordwrap(implode("\n", $messages), 70);
87 3
        $message = $this->composeMessage($body);
88 3
        if (!$message->send($this->mailer)) {
89 1
            throw new LogRuntimeException('Unable to export log through email!');
90
        }
91 2
    }
92
93
    /**
94
     * Composes a mail message with the given body content.
95
     * @param string $body the body content
96
     * @return \yii\mail\MessageInterface $message
97
     */
98 2
    protected function composeMessage($body)
99
    {
100 2
        $message = $this->mailer->compose();
101 2
        Yii::configure($message, $this->message);
102 2
        $message->setTextBody($body);
103
104 2
        return $message;
105
    }
106
}
107