Completed
Pull Request — master (#297)
by Anton
11:18
created

Mail::activation()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 27
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 47
rs 9.0303
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;
14
use Bluz\Proxy\Logger;
15
use Bluz\Proxy\Mailer;
16
use Bluz\Proxy\Router;
17
18
/**
19
 * Mail
20
 *
21
 * @package  Application\Users
22
 * @author   Anton Shevchuk
23
 */
24
class Mail
25
{
26
    /**
27
     * Activation
28
     *
29
     * @param Row    $user
30
     * @param string $password
31
     *
32
     * @return bool
33
     * @throws Exception
34
     */
35
    public static function activation($user, $password)
36
    {
37
        // email subject
38
        $subject = __('Activation');
39
40
        // create activation token
41
        // valid for 5 days
42
        $actionRow = UsersActions\Table::getInstance()->generate(
43
            $user->id,
44
            UsersActions\Table::ACTION_ACTIVATION
45
        );
46
47
        // send activation email
48
        // generate activation URL
49
        $activationUrl = Router::getFullUrl(
50
            'users',
51
            'activation',
52
            ['code' => $actionRow->code, 'id' => $user->id]
53
        );
54
55
        // generate mail template by controller
56
        $body = Application::getInstance()->dispatch(
57
            'users',
58
            'mail/template',
59
            [
60
                'template' => 'registration',
61
                'vars' => ['user' => $user, 'activationUrl' => $activationUrl, 'password' => $password]
62
            ]
63
        )->render();
64
65
        // try to send email
66
        try {
67
            $mail = Mailer::create();
68
            $mail->Subject = $subject;
69
            $mail->msgHTML(nl2br($body));
70
            $mail->addAddress($user->email);
71
            return Mailer::send($mail);
72
        } catch (\Exception $e) {
73
            Logger::log(
74
                'error',
75
                $e->getMessage(),
76
                ['module' => 'users', 'controller' => 'change-email', 'userId' => $user->id]
77
            );
78
79
            return false;
80
        }
81
    }
82
83
    /**
84
     * Password Recovery
85
     *
86
     * @param Row $user
87
     *
88
     * @return bool
89
     */
90
    public static function recovery($user)
91
    {
92
        // email subject
93
        $subject = __('Password Recovery');
94
95
        // create activation token
96
        // valid for 5 days
97
        $actionRow = UsersActions\Table::getInstance()->generate(
98
            $user->id,
99
            UsersActions\Table::ACTION_RECOVERY
100
        );
101
102
        // send activation email
103
        // generate restore URL
104
        $resetUrl = Router::getFullUrl(
105
            'users',
106
            'recovery-reset',
107
            ['code' => $actionRow->code, 'id' => $user->id]
108
        );
109
110
111
        $body = Application::getInstance()->dispatch(
112
            'users',
113
            'mail/template',
114
            [
115
                'template' => 'recovery',
116
                'vars' => ['user' => $user, 'resetUrl' => $resetUrl]
117
            ]
118
        )->render();
119
120
        try {
121
            $mail = Mailer::create();
122
            $mail->Subject = $subject;
123
            $mail->msgHTML(nl2br($body));
124
            $mail->addAddress($user->email);
125
126
            return Mailer::send($mail);
127
        } catch (\Exception $e) {
128
            // log it
129
            Logger::log(
130
                'error',
131
                $e->getMessage(),
132
                ['module' => 'users', 'controller' => 'recovery', 'email' => $user->email]
133
            );
134
            return false;
135
        }
136
    }
137
138
    /**
139
     * Change email
140
     *
141
     * @param Row    $user
142
     * @param string $email
143
     *
144
     * @return bool
145
     * @throws Exception
146
     */
147
    public static function changeEmail($user, $email)
148
    {
149
        // email subject
150
        $subject = __('Change email');
151
152
        // generate change mail token and get full url
153
        $actionRow = UsersActions\Table::getInstance()->generate(
154
            $user->id,
155
            UsersActions\Table::ACTION_CHANGE_EMAIL,
156
            5, // ttl in days
157
            ['email' => $email]
158
        );
159
160
        $changeUrl = Router::getFullUrl(
161
            'users',
162
            'change-email',
163
            ['token' => $actionRow->code]
164
        );
165
166
167
        $body = Application::getInstance()->dispatch(
168
            'users',
169
            'mail/template',
170
            [
171
                'template' => 'change-email',
172
                'vars' => [
173
                    'user' => $user,
174
                    'email' => $email,
175
                    'changeUrl' => $changeUrl,
176
                    'profileUrl' => Router::getFullUrl('users', 'profile')
177
                ]
178
            ]
179
        )->render();
180
181
        try {
182
            $mail = Mailer::create();
183
            $mail->Subject = $subject;
184
            $mail->msgHTML(nl2br($body));
185
            $mail->addAddress($email);
186
            return Mailer::send($mail);
187
188
        } catch (\Exception $e) {
189
            Logger::log(
190
                'error',
191
                $e->getMessage(),
192
                ['module' => 'users', 'controller' => 'change-email', 'userId' => $user->id]
193
            );
194
            return false;
195
        }
196
    }
197
}
198