Completed
Push — master ( 127008...59a424 )
by Charles
04:01
created

SendEmailEvent   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 31 5
1
<?php
2
3
namespace yrc\events;
4
use Yii;
5
6
final class SendEmailEvent extends \yrc\events\AbstractEvent
7
{
8
    public $viewFile;
9
    public $subject;
10
    public $destination;
11
    public $locales;
12
    public $viewParams;
13
14
    public function run()
15
    {
16
        // Default to this view file
17
        $viewFilePath = '@app/views/mail/en-US/' . $this->viewFile . '.twig';
18
        
19
        // Scan the "Accept-Language" header to see if we have a better viewfile
20
        foreach ($this->locales as $language) {
21
            $vfp = '@app/views/mail/' . $language . '/' . $viewFile . '.twig';
0 ignored issues
show
Bug introduced by
The variable $viewFile does not exist. Did you mean $viewFilePath?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
22
            if (\file_exists(Yii::getAlias($vfp))) {
23
                $viewFilePath = $vfp;
24
                break;
25
            }
26
        }
27
28
        if (!\file_exists(Yii::getAlias($viewFilePath))) {
29
            Yii::warning(sprintf('The requested view (%s) file does not exist', $viewFilePath));
30
            return false;
31
        }
32
33
        $view = Yii::$app->view->renderFile($viewFilePath, $this->viewParams);
34
        try {
35
            return Yii::$app->mailer->compose()
36
                ->setFrom(Yii::$app->yrc->fromEmail)
37
                ->setTo($this->destination)
38
                ->setSubject($this->subject)
39
                ->setHtmlBody($view)
40
                ->send();
41
        } catch (\Exception $e) {
42
            return false;
43
        }
44
    }
45
}