Completed
Push — master ( 8ea8de...59eb2e )
by Charles
01:52
created

AbstractEmailNotification   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compose() 0 21 1
A perform() 0 10 2
1
<?php declare(strict_types=1);
2
3
namespace app\jobs\notifications\email;
4
5
use RPQ\Server\AbstractJob;
6
use yii\mail\MessageInterface;
7
use Yii;
8
9
/**
10
 * @abstract AbstractEmailNotification
11
 * All email notifications should extend this abstract class to inherit basic queue based email sending capabilities
12
 * @property array $origin
13
 * @property string $subject
14
 * @property string $viewFile
15
 */
16
abstract class AbstractEmailNotification extends AbstractJob
17
{
18
    /**
19
     * The sender email address and name as an array.
20
     * @var array
21
     */
22
    protected $origin = [
23
        '[email protected]' => 'No Reply'
24
    ];
25
26
    /**
27
     * The email subject. This should be explicitly declared in extensions
28
     * @var string
29
     */
30
    protected $subject;
31
32
    /**
33
     * The name of the viewfile to use within @app/views/email/(html|text)
34
     * @var string
35
     */
36
    protected $viewFile;
37
38
    /**
39
     * Generates a message composition and returns a MessageInterface
40
     * @param string|array $to 
41
     * @param array $args
42
     * @return yii\mail\MessageInterface
43
     */
44
    protected function compose($to, array $args = []) : MessageInterface
45
    {
46
        // Grab the \yii\console\Controller instance, and setup the view paths
47
        // Yii::$app->mailer->compose() can't handle .twig extension, so the render/renderPartial methods need to be called manually
48
        $controller = Yii::$app->controller;
49
        $controller->setViewPath(Yii::$app->mailer->viewPath);
50
51
        // Compose a message, and return a MessageInterface if the consumer needs to modify the message interface in any way.
52
        $composition = Yii::$app->mailer->compose()
53
            ->setFrom($this->origin)
54
            ->setSubject(Yii::t('app', $this->subject)) // The subject should be translated, if possible
55
            ->setTo($to);
56
        
57
        $controller->layout = Yii::$app->mailer->htmlLayout;
58
        $composition->setHtmlBody($controller->render("html/{$this->viewFile}", $args));
59
60
        $controller->layout = Yii::$app->mailer->textLayout;
61
        $composition->setTextBody($controller->render("text/{$this->viewFile}", $args));
62
63
        return $composition;
64
    }
65
66
    /**
67
     * Implementation of RPQ\AbstractJob\Perform to send the actual email
68
     * @param array $args
69
     * @return int
70
     */
71
    public function perform(array $args = []) : int
72
    {
73
        $message = $this->compose($args['email'], $args);
74
        
75
        if ($message->send()) {
76
            return 0;
77
        }
78
79
        return 1;
80
    }
81
}