Completed
Push — master ( d0bd4d...b4b0fd )
by Igor
23:04
created

Notify   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 70%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 55
c 1
b 0
f 0
ccs 14
cts 20
cp 0.7
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
C sendMessage() 0 30 7
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 7
    public function init()
18
    {
19 7
        parent::init();
20
21 7
        $this->params = Yii::$app->settings;
22 7
    }
23
24
    /**
25
     * Send message
26
     *
27
     * @param string $to
28
     * @param string $subject
29
     * @param string $view
30
     * @param array $params
31
     * @param string[] $attach
32
     * @return bool
33
     */
34 7
    public function sendMessage(string $to, string $subject, string $view, array $params = [], array $attach = []): bool
35
    {
36 7
        $message = \Yii::$app->getMailer()->compose($view, $params);
37
38 7
        if (count($attach)) {
39
            foreach ($attach as $file) {
40
                $message->attach($file);
41
            }
42
        }
43
44 7
        if (strlen($this->params->emailMain) && strlen($this->params->emailName)) {
45 1
            $message->setFrom([$this->params->emailMain => $this->params->emailName]);
46
        }
47
48 7
        $to = explode(',', $to);
49 7
        $message->setTo($to);
50
51 7
        if (strlen($this->params->emailPrefix)) {
52
            $message->setSubject($this->params->emailPrefix . ': ' . $subject);
53
        } else {
54 7
            $message->setSubject($subject);
55
        }
56
57
        try {
58 7
            return $message->send();
59
        } catch (\Exception $e) {
60
            Yii::error($e);
61
            return false;
62
        }
63
    }
64
}
65