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

Notify   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 47
ccs 12
cts 16
cp 0.75
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
B sendMessage() 0 23 5
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