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

Mailer::sendMessage()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 37
ccs 20
cts 20
cp 1
rs 9.0111
cc 6
nc 32
nop 5
crap 6
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