Completed
Push — master ( 5d6787...430e93 )
by Wilmer
02:15
created

Mailer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace terabytesoft\helpers;
4
5
use yii\base\Component;
6
use yii\mail\MailerInterface;
7
8
/**
9
 * Class Mailer
10
 *
11
 **/
12
class Mailer extends Component
13
{
14
    /**
15
     * @var \yii\mail\MessageInterface $emailConfig
16
     */
17
    protected $emailConfig;
18
19
    /**
20
     * sendMessage
21
     */
22 3
    public function sendMessage(
23
        string $to,
24
        string $subject,
25
        array $options,
26
        array $params,
27
        MailerInterface $mailer
28
    ): bool {
29 3
        if (isset(\Yii::$app->params['helper.mailer.viewpath'])) {
30 3
            $mailer->viewPath = \Yii::$app->params['helper.mailer.viewpath'];
0 ignored issues
show
Bug introduced by
Accessing viewPath on the interface yii\mail\MailerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
31
        }
32
33 3
        $views = isset($options['views']) ? $options['views'] : [];
34
35 3
        $this->emailConfig = $mailer
36 3
            ->compose($views, $params)
37 3
            ->setTo($to)
38 3
            ->setFrom(
39 3
                [\Yii::$app->params['helper.mailer.sender'] => \Yii::$app->params['helper.mailer.sender.name']]
40
            )
41 3
            ->setSubject($subject);
42
43 3
        if (isset($options['replyTo'])) {
44 1
            $this->emailConfig = $this->emailConfig
45 1
                ->setReplyTo($options['replyTo']);
46
        }
47
48 3
        if (isset($options['textBody'])) {
49 2
            $this->emailConfig = $this->emailConfig
50 2
                ->setTextBody($options['textBody']);
51
        }
52
53 3
        if (isset($options['textHtml'])) {
54 1
            $this->emailConfig = $this->emailConfig
55 1
                ->setHtmlBody($options['textHtml']);
56
        }
57
58 3
        return $mailer->send($this->emailConfig);
59
    }
60
}
61