Completed
Push — master ( 46ddf1...45f408 )
by Vladimir
29:47
created

EmailTemplateBehavior::setSubject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-email-templates
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\email\templates\behaviors;
9
10
use Yii;
11
use yii\base\Behavior;
12
use yii\db\BaseActiveRecord;
13
use yii\helpers\Json;
14
use ymaker\email\templates\models\entities\EmailTemplate;
15
use ymaker\email\templates\models\entities\EmailTemplateTranslation;
16
17
/**
18
 * Behavior for appending of email templates to ActiveRecord models.
19
 *
20
 * @author Vladimir Kuprienko <[email protected]>
21
 * @since 3.0
22
 */
23
class EmailTemplateBehavior extends Behavior
24
{
25
    /**
26
     * @var BaseActiveRecord
27
     */
28
    public $owner;
29
30
    /**
31
     * @var string
32
     */
33
    private $_key = 'default';
34
    /**
35
     * @var string
36
     */
37
    private $_languageAttribute = 'language';
38
    /**
39
     * @var EmailTemplate
40
     */
41
    private $_template;
42
43
44
    /**
45
     * @param string $key
46
     */
47
    public function setKey($key)
48
    {
49
        $this->_key = $key;
50
    }
51
52
    /**
53
     * @param string $value
54
     */
55
    public function setLanguageAttribute($value)
56
    {
57
        $this->_languageAttribute = $value;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getSubject()
64
    {
65
        return $this->getTranslation()->subject;
66
    }
67
68
    /**
69
     * @param string $subject
70
     */
71
    public function setSubject($subject)
72
    {
73
        $this->getTranslation()->subject = $subject;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getBody()
80
    {
81
        return $this->getTranslation()->body;
82
    }
83
84
    /**
85
     * @param string $body
86
     */
87
    public function setBody($body)
88
    {
89
        $this->getTranslation()->body = $body;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getHint()
96
    {
97
        return $this->getTranslation()->hint;
98
    }
99
100
    /**
101
     * @param string $hint
102
     */
103
    public function setHint($hint)
104
    {
105
        $this->getTranslation()->hint = $hint;
106
    }
107
108
    /**
109
     * @return EmailTemplateTranslation
110
     */
111
    private function getTranslation()
112
    {
113
        $language = $this->owner->{$this->_languageAttribute};
114
        return $this->_template->getTranslation($language);
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function init()
121
    {
122
        $this->getEmailTemplate();
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function events()
129
    {
130
        return [
131
            BaseActiveRecord::EVENT_AFTER_FIND      => 'getEmailTemplate',
132
            BaseActiveRecord::EVENT_AFTER_INSERT    => 'saveEmailTemplate',
133
            BaseActiveRecord::EVENT_AFTER_UPDATE    => 'saveEmailTemplate',
134
            BaseActiveRecord::EVENT_AFTER_DELETE    => 'deleteEmailTemplate',
135
        ];
136
    }
137
138
    /**
139
     * Get email template from database.
140
     */
141
    public function getEmailTemplate()
142
    {
143
        if ($this->owner !== null) {
144
            $this->_template = EmailTemplate::find()
0 ignored issues
show
Documentation Bug introduced by
It seems like ymaker\email\templates\m...nguageAttribute)->one() can also be of type array. However, the property $_template is declared as type ymaker\email\templates\m...\entities\EmailTemplate. 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...
145
                ->byKey($this->generateKey())
146
                ->withTranslation($this->owner->{$this->_languageAttribute})
147
                ->one();
148
        } else {
149
            $this->_template = new EmailTemplate();
150
        }
151
    }
152
153
    /**
154
     * Save email template to database.
155
     */
156
    public function saveEmailTemplate()
157
    {
158
        if ($this->_template->key === null) {
159
            $this->_template->key = $this->generateKey();
160
        }
161
162
        try {
163
            $this->_template->save();
164
        } catch (\Exception $ex) {
165
            Yii::$app->getErrorHandler()->logException($ex);
166
        }
167
    }
168
169
    /**
170
     * Delete email template.
171
     */
172
    public function deleteEmailTemplate()
173
    {
174
        try {
175
            $this->_template->delete();
176
        } catch (\Exception $ex) {
177
            Yii::$app->getErrorHandler()->logException($ex);
178
        }
179
    }
180
181
    /**
182
     * Generates key for email template.
183
     *
184
     * @return string
185
     */
186
    protected function generateKey()
187
    {
188
        return Json::encode([
189
            'model' => get_class($this->owner),
190
            'id'    => $this->owner->getPrimaryKey(),
191
            'key'   => $this->_key,
192
        ]);
193
    }
194
}
195