Mailer   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
c 2
b 0
f 0
dl 0
loc 104
ccs 39
cts 39
cp 1
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setView() 0 3 2
A __construct() 0 4 1
A setMailer() 0 3 1
A setHtmlBody() 0 5 2
A setReplyTo() 0 5 2
A setTextBody() 0 5 2
A sendMessage() 0 17 1
A setViewPath() 0 4 2
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
     * @var object $mailer
21
     */
22
    protected $mailer;
23
24
    /**
25
     * @var array $view
26
     */
27
    protected $views;
28
29
    /**
30
     * __construct
31
     */
32 3
    public function __construct()
33
    {
34 3
        $this->setMailer();
35 3
        $this->setViewPath();
36 3
    }
37
38
    /**
39
     * sendMessage
40
     */
41 3
    public function sendMessage(string $to, string $subject, array $options = [], array $params = []): bool
42
    {
43 3
        $this->setView($options);
44
45 3
        $this->emailConfig = $this->mailer
46 3
            ->compose($this->views, $params)
47 3
            ->setTo($to)
48 3
            ->setFrom(
49 3
                [\Yii::$app->params['helper.mailer.sender'] => \Yii::$app->params['helper.mailer.sender.name']]
50
            )
51 3
            ->setSubject($subject);
52
53 3
        $this->setHtmlBody($options);
54 3
        $this->setReplyTo($options);
55 3
        $this->setTextBody($options);
56
57 3
        return $this->mailer->send($this->emailConfig);
58
    }
59
60
    /**
61
     * setHtmlBody
62
     */
63 3
    public function setHtmlBody(array $options): void
64
    {
65 3
        if (isset($options['htmlBody'])) {
66 1
            $this->emailConfig = $this->emailConfig
67 1
                ->setHtmlBody($options['htmlBody']);
68
        }
69 3
    }
70
71
    /**
72
     * setMailer
73
     */
74 3
    public function setMailer(): MailerInterface
75
    {
76 3
        return $this->mailer = \Yii::$app->mailer;
77
    }
78
79
    /**
80
     * setReplyTo
81
     */
82 3
    public function setReplyTo(array $options): void
83
    {
84 3
        if (isset($options['replyTo'])) {
85 1
            $this->emailConfig = $this->emailConfig
86 1
                ->setReplyTo($options['replyTo']);
87
        }
88 3
    }
89
90
    /**
91
     * setTextBody
92
     */
93 3
    public function setTextBody(array $options): void
94
    {
95 3
        if (isset($options['textBody'])) {
96 2
            $this->emailConfig = $this->emailConfig
97 2
                ->setTextBody($options['textBody']);
98
        }
99 3
    }
100
101
    /**
102
     * setView
103
     */
104 3
    public function setView(array $options): void
105
    {
106 3
        $this->views = isset($options['views']) ? $options['views'] : [];
107 3
    }
108
109
    /**
110
     * setViewPath
111
     */
112 3
    public function setViewPath(): void
113
    {
114 3
        if (isset(\Yii::$app->params['helper.mailer.viewpath'])) {
115 3
            $this->mailer->viewPath = \Yii::$app->params['helper.mailer.viewpath'];
116
        }
117 3
    }
118
}
119