Completed
Push — master ( e4197d...f733ed )
by Igor
11:05 queued 09:33
created

Notify::sendMessage()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.9256

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
ccs 8
cts 12
cp 0.6667
rs 8.5906
c 1
b 0
f 0
cc 5
eloc 14
nc 8
nop 4
crap 5.9256
1
<?php
2
3
namespace app\components;
4
5
use Yii;
6
7
/**
8
 * Component for notify user
9
 */
10
class Notify extends \yii\base\Component
11
{
12
    /**
13
     * @var object
14
     */
15
    private $params = null;
16
17 6
    public function init()
18
    {
19 6
        parent::init();
20
21 6
        $this->params = Yii::$app->settings;
22 6
    }
23
24
    /**
25
     * Send message
26
     *
27
     * @param string $to
28
     * @param string $subject
29
     * @param string $view
30
     * @param array $params
31
     * @return bool
32
     */
33 6
    public function sendMessage(string $to, string $subject, string $view, array $params = []): bool
34
    {
35 6
        $message = \Yii::$app->getMailer()->compose($view, $params);
36
37 6
        if (strlen($this->params->emailMain) && strlen($this->params->emailName)) {
38 1
            $message->setFrom([$this->params->emailMain => $this->params->emailName]);
39
        }
40
41 6
        $message->setTo($to);
42
43 6
        if (strlen($this->params->emailPrefix)) {
44
            $message->setSubject($this->params->emailPrefix . ': ' . $subject);
45
        } else {
46 6
            $message->setSubject($subject);
47
        }
48
49
        try {
50 6
            return $message->send();
51
        } catch (\Exception $e) {
52
            Yii::error($e);
53
            return false;
54
        }
55
    }
56
}
57