AbstractEmailNotification   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A perform() 0 9 2
A compose() 0 20 1
1
<?php declare(strict_types=1);
2
3
namespace yrc\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
    abstract protected function getSubject();
39
40
    /**
41
     * Generates a message composition and returns a MessageInterface
42
     * @param string|array $to
43
     * @param array $args
44
     * @return yii\mail\MessageInterface
45
     */
46
    protected function compose($to, array $args = []) : MessageInterface
47
    {
48
        // Grab the \yii\console\Controller instance, and setup the view paths
49
        // Yii::$app->mailer->compose() can't handle .twig extension, so the render/renderPartial methods need to be called manually
50
        $controller = Yii::$app->controller;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->controller can also be of type yii\web\Controller. However, the property $controller is declared as type yii\console\Controller. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
51
        $controller->setViewPath(Yii::$app->mailer->viewPath);
0 ignored issues
show
Bug introduced by
Accessing viewPath on the interface yii\mail\MailerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
52
53
        // Compose a message, and return a MessageInterface if the consumer needs to modify the message interface in any way.
54
        $composition = Yii::$app->mailer->compose()
55
            ->setFrom($args['origin'] ?? $this->origin)
56
            ->setSubject(Yii::t('app', $this->getSubject())) // The subject should be translated, if possible
57
            ->setTo($to);
58
        
59
        $controller->layout = Yii::$app->mailer->htmlLayout;
0 ignored issues
show
Bug introduced by
Accessing htmlLayout on the interface yii\mail\MailerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
60
        $composition->setHtmlBody($controller->render("html/{$this->viewFile}", $args));
61
62
        $controller->layout = Yii::$app->mailer->textLayout;
0 ignored issues
show
Bug introduced by
Accessing textLayout on the interface yii\mail\MailerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
63
        $composition->setTextBody($controller->render("text/{$this->viewFile}", $args));
64
65
        return $composition;
66
    }
67
68
    /**
69
     * Implementation of RPQ\AbstractJob\Perform to send the actual email
70
     * @param array $args
71
     * @return int
72
     */
73
    public function perform(array $args = []) : int
74
    {
75
        $message = $this->compose($args['email'], $args);
76
        
77
        if ($message->send()) {
78
            return 0;
79
        }
80
81
        return 1;
82
    }
83
}
84