Failed Conditions
Pull Request — master (#321)
by Anton
23:35 queued 08:33
created

Mail::activation()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 26
c 2
b 0
f 0
nc 5
nop 2
dl 0
loc 46
ccs 0
cts 33
cp 0
crap 6
rs 9.504
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
declare(strict_types=1);
8
9
namespace Application\Users;
10
11
use Application\Exception;
12
use Application\UsersActions;
13
use Bluz\Application\Application;
0 ignored issues
show
Bug introduced by
The type Bluz\Application\Application 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...
14
use Bluz\Proxy\Logger;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Logger 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...
15
use Bluz\Proxy\Mailer;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Mailer 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...
16
use Bluz\Proxy\Router;
0 ignored issues
show
Bug introduced by
The type Bluz\Proxy\Router 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...
17
18
/**
19
 * Mail for Users
20
 *
21
 * @package  Application\Users
22
 */
23
class Mail
24
{
25
    /**
26
     * Activation
27
     *
28
     * @param Row $user
29
     * @param string $password
30
     *
31
     * @return bool
32
     * @throws \Bluz\Common\Exception\ComponentException
33
     * @throws \Bluz\Common\Exception\CommonException
34
     * @throws \Bluz\Controller\ControllerException
35
     * @throws \Bluz\Http\Exception\NotAllowedException
36
     * @throws \Bluz\Http\Exception\NotAcceptableException
37
     * @throws \Bluz\Http\Exception\ForbiddenException
38
     */
39
    public static function activation($user, $password): ?bool
40
    {
41
        // email subject
42
        $subject = __('Activation');
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $subject = /** @scrutinizer ignore-call */ __('Activation');
Loading history...
43
44
        // create activation token
45
        // valid for 5 days
46
        $actionRow = UsersActions\Table::getInstance()->generate(
47
            $user->id,
48
            UsersActions\Table::ACTION_ACTIVATION
49
        );
50
51
        // send activation email
52
        // generate activation URL
53
        $activationUrl = Router::getFullUrl(
54
            'users',
55
            'activation',
56
            ['code' => $actionRow->code, 'id' => $user->id]
57
        );
58
59
        // generate mail template by controller
60
        $body = Application::getInstance()->dispatch(
61
            'users',
62
            'mail/template',
63
            [
64
                'template' => 'registration',
65
                'vars' => ['user' => $user, 'activationUrl' => $activationUrl, 'password' => $password]
66
            ]
67
        )->render();
68
69
        // try to send email
70
        try {
71
            $mail = Mailer::create();
72
            $mail->Subject = $subject;
73
            $mail->msgHTML(nl2br($body));
74
            $mail->addAddress($user->email);
75
76
            return Mailer::send($mail);
77
        } catch (\Exception $e) {
78
            Logger::log(
79
                'error',
80
                $e->getMessage(),
81
                ['module' => 'users', 'controller' => 'change-email', 'userId' => $user->id]
82
            );
83
84
            return false;
85
        }
86
    }
87
88
    /**
89
     * Password Recovery
90
     *
91
     * @param Row $user
92
     *
93
     * @return bool
94
     * @throws \Bluz\Common\Exception\ComponentException
95
     * @throws \Bluz\Common\Exception\CommonException
96
     * @throws \Bluz\Controller\ControllerException
97
     * @throws \Bluz\Http\Exception\NotAllowedException
98
     * @throws \Bluz\Http\Exception\NotAcceptableException
99
     * @throws \Bluz\Http\Exception\ForbiddenException
100
     */
101
    public static function recovery($user): ?bool
102
    {
103
        // email subject
104
        $subject = __('Password Recovery');
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
        $subject = /** @scrutinizer ignore-call */ __('Password Recovery');
Loading history...
105
106
        // create activation token
107
        // valid for 5 days
108
        $actionRow = UsersActions\Table::getInstance()->generate(
109
            $user->id,
110
            UsersActions\Table::ACTION_RECOVERY
111
        );
112
113
        // send activation email
114
        // generate restore URL
115
        $resetUrl = Router::getFullUrl(
116
            'users',
117
            'recovery-reset',
118
            ['code' => $actionRow->code, 'id' => $user->id]
119
        );
120
121
122
        $body = Application::getInstance()->dispatch(
123
            'users',
124
            'mail/template',
125
            [
126
                'template' => 'recovery',
127
                'vars' => ['user' => $user, 'resetUrl' => $resetUrl]
128
            ]
129
        )->render();
130
131
        try {
132
            $mail = Mailer::create();
133
            $mail->Subject = $subject;
134
            $mail->msgHTML(nl2br($body));
135
            $mail->addAddress($user->email);
136
137
            return Mailer::send($mail);
138
        } catch (\Exception $e) {
139
            // log it
140
            Logger::log(
141
                'error',
142
                $e->getMessage(),
143
                ['module' => 'users', 'controller' => 'recovery', 'email' => $user->email]
144
            );
145
            return false;
146
        }
147
    }
148
149
    /**
150
     * Change email
151
     *
152
     * @param Row $user
153
     * @param string $email
154
     *
155
     * @return bool
156
     * @throws \Bluz\Common\Exception\ComponentException
157
     * @throws \Bluz\Common\Exception\CommonException
158
     * @throws \Bluz\Controller\ControllerException
159
     * @throws \Bluz\Http\Exception\NotAllowedException
160
     * @throws \Bluz\Http\Exception\NotAcceptableException
161
     * @throws \Bluz\Http\Exception\ForbiddenException
162
     */
163
    public static function changeEmail($user, $email): ?bool
164
    {
165
        // email subject
166
        $subject = __('Change email');
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

166
        $subject = /** @scrutinizer ignore-call */ __('Change email');
Loading history...
167
168
        // generate change mail token and get full url
169
        $actionRow = UsersActions\Table::getInstance()->generate(
170
            $user->id,
171
            UsersActions\Table::ACTION_CHANGE_EMAIL,
172
            5, // ttl in days
173
            ['email' => $email]
174
        );
175
176
        $changeUrl = Router::getFullUrl(
177
            'users',
178
            'change-email',
179
            ['token' => $actionRow->code]
180
        );
181
182
        $body = Application::getInstance()->dispatch(
183
            'users',
184
            'mail/template',
185
            [
186
                'template' => 'change-email',
187
                'vars' => [
188
                    'user' => $user,
189
                    'email' => $email,
190
                    'changeUrl' => $changeUrl,
191
                    'profileUrl' => Router::getFullUrl('users', 'profile')
192
                ]
193
            ]
194
        )->render();
195
196
        try {
197
            $mail = Mailer::create();
198
            $mail->Subject = $subject;
199
            $mail->msgHTML(nl2br($body));
200
            $mail->addAddress($email);
201
202
            return Mailer::send($mail);
203
        } catch (\Exception $e) {
204
            Logger::log(
205
                'error',
206
                $e->getMessage(),
207
                ['module' => 'users', 'controller' => 'change-email', 'userId' => $user->id]
208
            );
209
            return false;
210
        }
211
    }
212
}
213