MailChannel   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 11
c 1
b 0
f 0
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A send() 0 11 2
1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
namespace tuyakhov\notifications\channels;
6
7
8
use tuyakhov\notifications\messages\MailMessage;
9
use tuyakhov\notifications\NotifiableInterface;
10
use tuyakhov\notifications\NotificationInterface;
11
use yii\base\Component;
12
use yii\di\Instance;
13
use yii\mail\MailerInterface;
14
15
class MailChannel extends Component implements ChannelInterface
16
{
17
    /**
18
     * @var $mailer MailerInterface|array|string the mailer object or the application component ID of the mailer object.
0 ignored issues
show
Documentation Bug introduced by
The doc comment $mailer at position 0 could not be parsed: Unknown type name '$mailer' at position 0 in $mailer.
Loading history...
19
     */
20
    public $mailer = 'mailer';
21
22
    /**
23
     * The message sender.
24
     * @var string
25
     */
26
    public $from;
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function init()
32
    {
33
        parent::init();
34
        $this->mailer = Instance::ensure($this->mailer, 'yii\mail\MailerInterface');
35
    }
36
    
37
    public function send(NotifiableInterface $recipient, NotificationInterface $notification)
38
    {
39
        /**
40
         * @var $message MailMessage
41
         */
42
        $message = $notification->exportFor('mail');
43
        return $this->mailer->compose($message->view, $message->viewData)
44
            ->setFrom(isset($message->from) ? $message->from : $this->from)
45
            ->setTo($recipient->routeNotificationFor('mail'))
46
            ->setSubject($message->subject)
47
            ->send();
48
    }
49
}
50