Completed
Push — master ( 547f5e...f26d15 )
by Wilmer
03:02
created

Mailer::setConfirmationSubject()   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
namespace terabytesoft\mailer\user;
4
5
use terabytesoft\app\user\models\TokenModel;
0 ignored issues
show
Bug introduced by
The type terabytesoft\app\user\models\TokenModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use terabytesoft\app\user\models\UserModel;
0 ignored issues
show
Bug introduced by
The type terabytesoft\app\user\models\UserModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use terabytesoft\app\user\traits\ModuleTrait;
0 ignored issues
show
Bug introduced by
The type terabytesoft\app\user\traits\ModuleTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use yii\base\Component;
9
10
/**
11
 * Class Mailer.
12
 *
13
 **/
14
class Mailer extends Component
15
{
16
    use ModuleTrait;
17
18
    public $mailerComponent;
19
    public $sender;
20
    public $viewPath = '@terabytesoft/mailer/user/views';
21
22
    protected $confirmationSubject;
23
    protected $newPasswordSubject;
24
    protected $reconfirmationSubject;
25
    protected $recoverySubject;
26
    protected $welcomeSubject;
27
28
29
    /**
30
     * getWelcomeSubject.
31
     *
32
     * @return string
33
     **/
34
    public function getWelcomeSubject(): string
35
    {
36
        if ($this->welcomeSubject == null) {
37
            $this->setWelcomeSubject(\Yii::t(
38
                'app.user',
39
                'Welcome to {0}',
40
                [$this->app->name]
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
41
            ));
42
        }
43
44
        return $this->welcomeSubject;
45
    }
46
47
    /**
48
     * setWelcomeSubject.
49
     *
50
     * @param string $welcomeSubject
51
     *
52
     * @return string
53
     **/
54
    public function setWelcomeSubject(string $welcomeSubject)
55
    {
56
        $this->welcomeSubject = $welcomeSubject;
57
    }
58
59
    /**
60
     * getNewPasswordSubject.
61
     *
62
     * @return string
63
     **/
64
    public function getNewPasswordSubject(): string
65
    {
66
        if ($this->newPasswordSubject == null) {
67
            $this->setNewPasswordSubject(\Yii::t(
68
                'app.user',
69
                'Your password on {0} has been changed',
70
                [$this->app->name]
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
71
            ));
72
        }
73
74
        return $this->newPasswordSubject;
75
    }
76
77
    /**
78
     * setNewPasswordSubject.
79
     *
80
     * @param string $newPasswordSubject
81
     *
82
     * @return string
83
     **/
84
    public function setNewPasswordSubject(string $newPasswordSubject)
85
    {
86
        $this->newPasswordSubject = $newPasswordSubject;
87
    }
88
89
    /**
90
     * getConfirmationSubject.
91
     *
92
     * @return string
93
     **/
94
    public function getConfirmationSubject(): string
95
    {
96
        if ($this->confirmationSubject == null) {
97
            $this->setConfirmationSubject(\Yii::t(
98
                'app.user',
99
                'Confirm account on {0}',
100
                [$this->app->name]
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
101
            ));
102
        }
103
104
        return $this->confirmationSubject;
105
    }
106
107
    /**
108
     * setConfirmationSubject.
109
     *
110
     * @param string $newPasswordSubject
111
     *
112
     * @return string
113
     **/
114
    public function setConfirmationSubject(string $confirmationSubject)
115
    {
116
        $this->confirmationSubject = $confirmationSubject;
117
    }
118
119
    /**
120
     * getReconfirmationSubject.
121
     *
122
     * @return string
123
     **/
124
    public function getReconfirmationSubject(): string
125
    {
126
        if ($this->reconfirmationSubject == null) {
127
            $this->setReconfirmationSubject(\Yii::t(
128
                'app.user',
129
                'Confirm email change on {0}',
130
                [$this->app->name]
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
131
            ));
132
        }
133
134
        return $this->reconfirmationSubject;
135
    }
136
137
    /**
138
     * setReconfirmationSubject.
139
     *
140
     * @param string $reconfirmationSubject
141
     *
142
     * @return string
143
     **/
144
    public function setReconfirmationSubject($reconfirmationSubject)
145
    {
146
        $this->reconfirmationSubject = $reconfirmationSubject;
147
    }
148
149
    /**
150
     * getRecoverySubject.
151
     *
152
     * @return string
153
     **/
154
    public function getRecoverySubject()
155
    {
156
        if ($this->recoverySubject == null) {
157
            $this->setRecoverySubject(\Yii::t(
158
                'app.user',
159
                'Complete password reset on {0}',
160
                [$this->app->name]
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
161
            ));
162
        }
163
164
        return $this->recoverySubject;
165
    }
166
167
    /**
168
     * setRecoverySubject.
169
     *
170
     * @param string $recoverySubject
171
     *
172
     * @return string|null
173
     **/
174
    public function setRecoverySubject(string $recoverySubject)
175
    {
176
        $this->recoverySubject = $recoverySubject;
177
    }
178
179
    /**
180
     * sendWelcomeMessage.
181
     *
182
     * sends an email to a user after registration.
183
     *
184
     * @param UserModel $user
185
     * @param TokenModel $token
186
     * @param bool $showPassword
187
     *
188
     * @return bool
189
     **/
190
    public function sendWelcomeMessage(UserModel $user, TokenModel $token = null, $showPassword = false): bool
191
    {
192
        return $this->sendMessage(
193
            $user->email,
194
            $this->getWelcomeSubject(),
195
            'welcome',
196
            ['user' => $user, 'token' => $token, 'module' => $this->module, 'showPassword' => $showPassword]
0 ignored issues
show
Bug Best Practice introduced by
The property module does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
197
        );
198
    }
199
200
    /**
201
     * sendGeneratedPassword.
202
     *
203
     * sends a new generated password to a user
204
     *
205
     * @param UserModel $user
206
     * @param \app\user\helpers\PasswordHelper $password
0 ignored issues
show
Bug introduced by
The type app\user\helpers\PasswordHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
207
     *
208
     * @return bool
209
     **/
210
    public function sendGeneratedPassword(UserModel $user, $password): bool
211
    {
212
        return $this->sendMessage(
213
            $user->email,
214
            $this->getNewPasswordSubject(),
215
            'New_Password',
216
            ['user' => $user, 'password' => $password, 'module' => $this->module]
0 ignored issues
show
Bug Best Practice introduced by
The property module does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
217
        );
218
    }
219
220
    /**
221
     * sendConfirmationMessage.
222
     *
223
     * sends an email to a user with confirmation link
224
     *
225
     * @param UserModel $user
226
     * @param TokenModel $token
227
     *
228
     * @return bool
229
     **/
230
    public function sendConfirmationMessage(UserModel $user, TokenModel $token): bool
231
    {
232
        return $this->sendMessage(
233
            $user->email,
234
            $this->getConfirmationSubject(),
235
            'Confirmation',
236
            ['user' => $user, 'token' => $token, 'module' => $this->module]
0 ignored issues
show
Bug Best Practice introduced by
The property module does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
237
        );
238
    }
239
240
    /**
241
     * sendReconfirmationMessage.
242
     *
243
     * sends an email to a user with reconfirmation link.
244
     *
245
     * @param UserModel $user
246
     * @param TokenModel $token
247
     *
248
     * @return bool
249
     **/
250
    public function sendReconfirmationMessage(UserModel $user, TokenModel $token): bool
251
    {
252
        if ($token->type == TokenModel::TYPE_CONFIRM_NEW_EMAIL) {
253
            $email = $user->unconfirmed_email;
254
        } else {
255
            $email = $user->email;
256
        }
257
258
        return $this->sendMessage(
259
            $email,
260
            $this->getReconfirmationSubject(),
261
            'Reconfirmation',
262
            ['user' => $user, 'token' => $token, 'module' => $this->module]
0 ignored issues
show
Bug Best Practice introduced by
The property module does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
263
        );
264
    }
265
266
    /**
267
     * sendRecoveryMessage.
268
     *
269
     * sends an email to a user with recovery link
270
     *
271
     * @param UserModel $user
272
     * @param TokenModel $token
273
     *
274
     * @return bool
275
     **/
276
    public function sendRecoveryMessage(UserModel $user, TokenModel $token)
277
    {
278
        $this->app->session->set('sendRecoveryMessage', true);
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
279
        return $this->sendMessage(
280
            $user->email,
281
            $this->getRecoverySubject(),
282
            'Recovery',
283
            ['user' => $user, 'token' => $token, 'module' => $this->module]
0 ignored issues
show
Bug Best Practice introduced by
The property module does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
284
        );
285
    }
286
287
    /**
288
     * sendMessage.
289
     *
290
     * @param string $to
291
     * @param string $subject
292
     * @param string $view
293
     * @param array  $params
294
     *
295
     * @return bool
296
     **/
297
    protected function sendMessage($to, $subject, $view, $params = []): bool
298
    {
299
        $mailer = $this->mailerComponent === null ? $this->app->mailer : $this->app->get($this->mailerComponent);
0 ignored issues
show
Bug Best Practice introduced by
The property app does not exist on terabytesoft\mailer\user\Mailer. Since you implemented __get, consider adding a @property annotation.
Loading history...
300
301
        $mailer->viewPath = $this->viewPath;
302
303
        if ($this->sender === null) {
304
            $this->sender = isset($this->app->params['adminEmail']) ?
305
                $this->app->params['adminEmail']
306
                : '[email protected]';
307
        }
308
309
        return $mailer->compose(['html' => $view, 'text' => 'text/' . $view], $params)
310
            ->setTo($to)
311
            ->setFrom($this->sender)
312
            ->setSubject($subject)
313
            ->send();
314
    }
315
}
316