EmailTarget::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * Health monitoring for Yii2 applications
4
 *
5
 * @link      https://github.com/hiqdev/yii2-monitoring
6
 * @package   yii2-monitoring
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\monitoring\targets;
12
13
use hiqdev\yii2\monitoring\Module;
14
use yii\mail\MailerInterface;
15
16
class EmailTarget extends \yii\log\Target
17
{
18
    public $view;
19
20
    public $from;
21
    public $to;
22
    public $subject;
23
24
    /**
25
     * @var MailerInterface
26
     */
27
    private $mailer;
28
29
    public function __construct(MailerInterface $mailer, $options = [])
30
    {
31
        parent::__construct($options);
32
        $this->mailer = $mailer;
33
    }
34
35
    public function export()
36
    {
37
        $module = Module::getInstance();
38
39
        foreach ($this->messages as $message) {
40
            return $this->mailer
41
                ->compose($this->view, $module->prepareMessageData($message, $this))
42
                ->setTo($this->to)
43
                ->setFrom($module->flagEmail($this->from))
44
                ->setSubject($module->prepareSubject($message, $this->subject))
45
                ->send();
46
        }
47
    }
48
}
49