Completed
Push — master ( ebefd5...98eed5 )
by Charles
06:43
created

YRC   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 57
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B sendEmail() 0 28 4
1
<?php
2
3
namespace yrc\components;
4
5
use Yii;
6
use yii\base\Object;
7
8
/**
9
 * Yii2 Rest Component 
10
 * @class YRC
11
 */
12
class YRC extends Object
13
{
14
    /**
15
     * The user class
16
     * @var string
17
     */
18
    public $userClass;
19
20
    /**
21
     * The email address that emails should be sent from
22
     * @var string
23
     */
24
    public $fromEmail;
25
26
    /**
27
     * Whether or not we should really send emails
28
     * @var boolean
29
     */
30
    public $realSend = true;
31
32
    /**
33
     * Send an email using the provided viewfile and parameters
34
     * @param string $viewFile
35
     * @param string $subject
36
     * @param string $email
37
     * @param array $params
38
     * @return boolean
39
     */
40
    public function sendEmail($viewFile, $subject, $email, array $params = [])
41
    {
42
        // Default to this view file
43
        $viewFilePath = '@app/views/mail/en-US/' . $viewFile . '.twig';
44
        
45
        // Scan the "Accept-Language" header to see if we have a better viewfile
46
        $languages = Yii::$app->request->getAcceptableLanguages();
47
        foreach ($languages as $language) {
48
            $vfp = '@app/views/mail/' . $language . '/' . $viewFile . '.twig';
49
            if (\file_exists(Yii::getAlias($vfp))) {
50
                $viewFilePath = $vfp;
51
                break;
52
            }
53
        }
54
55
        $view = Yii::$app->view->renderFile($viewFilePath, $params);
56
57
        if ($this->realSend === true) {
58
            return Yii::$app->mailer->compose()
59
                ->setFrom($this->fromEmail)
60
                ->setTo($email)
61
                ->setSubject($subject)
62
                ->setHtmlBody($view)
63
                ->send();
64
        }
65
66
        return true;
67
    }
68
}