Completed
Push — master ( 033323...4b3771 )
by Vladimir
02:26
created

EmailTemplateBehavior::setLetterSubject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
 * @property string $letterSubject
21
 * @property string $letterBody
22
 * @property string $emailTemplateHint
23
 *
24
 * @author Vladimir Kuprienko <[email protected]>
25
 * @since 3.0
26
 */
27
class EmailTemplateBehavior extends Behavior
28
{
29
    /**
30
     * @var BaseActiveRecord
31
     */
32
    public $owner;
33
34
    /**
35
     * @var string
36
     */
37
    private $_key = 'default';
38
    /**
39
     * @var string
40
     */
41
    private $_languageAttribute = 'language';
42
    /**
43
     * @var EmailTemplate
44
     */
45
    private $_template;
46
47
48
    /**
49
     * @param string $key
50
     */
51
    public function setKey($key)
52
    {
53
        $this->_key = $key;
54
    }
55
56
    /**
57
     * @param string $value
58
     */
59
    public function setLanguageAttribute($value)
60
    {
61
        $this->_languageAttribute = $value;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getLetterSubject()
68
    {
69
        return $this->getTranslation()->subject;
70
    }
71
72
    /**
73
     * @param string $subject
74
     */
75
    public function setLetterSubject($subject)
76
    {
77
        $this->getTranslation()->subject = $subject;
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getLetterBody()
84
    {
85
        return $this->getTranslation()->body;
86
    }
87
88
    /**
89
     * @param string $body
90
     */
91
    public function setLetterBody($body)
92
    {
93
        $this->getTranslation()->body = $body;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getEmailTemplateHint()
100
    {
101
        return $this->getTranslation()->hint;
102
    }
103
104
    /**
105
     * @param string $hint
106
     */
107
    public function setEmailTemplateHint($hint)
108
    {
109
        $this->getTranslation()->hint = $hint;
110
    }
111
112
    /**
113
     * @return EmailTemplateTranslation
114
     */
115
    private function getTranslation()
116
    {
117
        $language = $this->owner->{$this->_languageAttribute};
118
        return $this->_template->getTranslation($language);
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function init()
125
    {
126
        $this->getEmailTemplate();
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132
    public function events()
133
    {
134
        return [
135
            BaseActiveRecord::EVENT_AFTER_FIND      => 'getEmailTemplate',
136
            BaseActiveRecord::EVENT_AFTER_INSERT    => 'saveEmailTemplate',
137
            BaseActiveRecord::EVENT_AFTER_UPDATE    => 'saveEmailTemplate',
138
            BaseActiveRecord::EVENT_AFTER_DELETE    => 'deleteEmailTemplate',
139
        ];
140
    }
141
142
    /**
143
     * Get email template from database.
144
     */
145
    public function getEmailTemplate()
146
    {
147
        if ($this->owner !== null) {
148
            $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...
149
                ->byKey($this->generateKey())
150
                ->withTranslation($this->owner->{$this->_languageAttribute})
151
                ->one();
152
        } else {
153
            $this->_template = new EmailTemplate();
154
        }
155
    }
156
157
    /**
158
     * Save email template to database.
159
     */
160
    public function saveEmailTemplate()
161
    {
162
        if ($this->_template->key === null) {
163
            $this->_template->key = $this->generateKey();
164
        }
165
166
        try {
167
            $this->_template->save();
168
        } catch (\Exception $ex) {
169
            Yii::$app->getErrorHandler()->logException($ex);
170
        }
171
    }
172
173
    /**
174
     * Delete email template.
175
     */
176
    public function deleteEmailTemplate()
177
    {
178
        try {
179
            $this->_template->delete();
180
        } catch (\Exception $ex) {
181
            Yii::$app->getErrorHandler()->logException($ex);
182
        }
183
    }
184
185
    /**
186
     * Generates key for email template.
187
     *
188
     * @return string
189
     */
190
    protected function generateKey()
191
    {
192
        return Json::encode([
193
            'model' => get_class($this->owner),
194
            'id'    => $this->owner->getPrimaryKey(),
195
            'key'   => $this->_key,
196
        ]);
197
    }
198
}
199