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

Notify::sendMessage()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9.5839

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 30
c 1
b 0
f 0
ccs 10
cts 16
cp 0.625
rs 6.7272
cc 7
eloc 18
nc 16
nop 5
crap 9.5839
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