Passed
Push — master ( 291909...8bbd8c )
by Vladimir
03:56
created

EmailTemplateBehavior::setLetterBody()   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-2018 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\email\templates\behaviors;
9
10
use yii\base\Behavior;
11
use yii\db\BaseActiveRecord;
12
use yii\helpers\Json;
13
use ymaker\email\templates\repositories\EmailTemplatesRepositoryInterface;
14
15
/**
16
 * Behavior for appending of email templates to ActiveRecord models.
17
 *
18
 * @property string $letterSubject
19
 * @property string $letterBody
20
 * @property string $emailTemplateHint
21
 *
22
 * @author Vladimir Kuprienko <[email protected]>
23
 * @since 3.0
24
 */
25
class EmailTemplateBehavior extends Behavior
26
{
27
    /**
28
     * @var BaseActiveRecord
29
     */
30
    public $owner;
31
32
    /**
33
     * @var EmailTemplatesRepositoryInterface
34
     */
35
    private $_repository;
36
    /**
37
     * @var string
38
     */
39
    private $_key = 'default';
40
    /**
41
     * @var string
42
     */
43
    private $_languageAttribute = 'language';
44
    /**
45
     * @var \ymaker\email\templates\entities\EmailTemplate
46
     */
47
    private $_template;
48
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function __construct(
54
        EmailTemplatesRepositoryInterface $repository,
55
        $config = []
56
    ) {
57
        $this->_repository = $repository;
58
59
        parent::__construct($config);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function init()
66
    {
67
        $this->fetchEmailTemplate();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function events()
74
    {
75
        return [
76
            BaseActiveRecord::EVENT_AFTER_FIND      => 'fetchEmailTemplate',
77
            BaseActiveRecord::EVENT_AFTER_INSERT    => 'saveEmailTemplate',
78
            BaseActiveRecord::EVENT_AFTER_UPDATE    => 'saveEmailTemplate',
79
            BaseActiveRecord::EVENT_AFTER_DELETE    => 'deleteEmailTemplate',
80
        ];
81
    }
82
83
    /**
84
     * @param string $key
85
     */
86
    public function setKey($key)
87
    {
88
        $this->_key = $key;
89
    }
90
91
    /**
92
     * @param string $value
93
     */
94
    public function setLanguageAttribute($value)
95
    {
96
        $this->_languageAttribute = $value;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function getLetterSubject()
103
    {
104
        return $this->getTranslation()->subject;
105
    }
106
107
    /**
108
     * @param string $subject
109
     */
110
    public function setLetterSubject($subject)
111
    {
112
        $this->getTranslation()->subject = $subject;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getLetterBody()
119
    {
120
        return $this->getTranslation()->body;
121
    }
122
123
    /**
124
     * @param string $body
125
     */
126
    public function setLetterBody($body)
127
    {
128
        $this->getTranslation()->body = $body;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getEmailTemplateHint()
135
    {
136
        return $this->getTranslation()->hint;
137
    }
138
139
    /**
140
     * @param string $hint
141
     */
142
    public function setEmailTemplateHint($hint)
143
    {
144
        $this->getTranslation()->hint = $hint;
145
    }
146
147
    /**
148
     * Fetch email template from database.
149
     */
150
    public function fetchEmailTemplate()
151
    {
152
        if (null !== $this->owner) {
153
            $this->_template = $this->_repository->getByKeyWithTranslation(
154
                $this->generateKey(),
155
                $this->owner->{$this->_languageAttribute}
156
            );
157
        } else {
158
            $this->_template = $this->_repository->create();
159
        }
160
    }
161
162
    /**
163
     * Save email template to database.
164
     */
165
    public function saveEmailTemplate()
166
    {
167
        if (null === $this->_template->key) {
168
            $this->_template->key = $this->generateKey();
169
        }
170
171
        $this->_repository->save($this->_template);
172
    }
173
174
    /**
175
     * Delete email template.
176
     */
177
    public function deleteEmailTemplate()
178
    {
179
        $this->_repository->deleteObject($this->_template);
180
    }
181
182
    /**
183
     * Generates key for email template.
184
     *
185
     * @return string
186
     */
187
    protected function generateKey()
188
    {
189
        return Json::encode([
190
            'model' => get_class($this->owner),
191
            'id'    => $this->owner->getPrimaryKey(),
192
            'key'   => $this->_key,
193
        ]);
194
    }
195
196
    /**
197
     * @return \ymaker\email\templates\entities\EmailTemplateTranslation
198
     */
199
    private function getTranslation()
200
    {
201
        $language = $this->owner->{$this->_languageAttribute};
202
203
        return $this->_template->getTranslation($language);
204
    }
205
}
206