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

Notify::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
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