Passed
Pull Request — master (#15)
by Maximo
04:01
created

AuthController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 44.83%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 56
ccs 13
cts 29
cp 0.4483
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onConstruct() 0 7 2
A sendEmail() 0 33 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Gewaer\Models\Users;
8
use Baka\Auth\Models\Users as BakaUsers;
9
use Gewaer\Models\UserLinkedSources;
10
use Gewaer\Exception\ServerErrorHttpException;
11
12
/**
13
 * Class AuthController
14
 *
15
 * @package Gewaer\Api\Controllers
16
 *
17
 * @property Users $userData
18
 * @property Request $request
19
 * @property Config $config
20
 * @property \Baka\Mail\Message $mail
21
 */
22
class AuthController extends \Baka\Auth\AuthController
23
{
24
    /**
25
     * Setup for this controller
26
     *
27
     * @return void
28
     */
29 11
    public function onConstruct()
30
    {
31 11
        $this->userLinkedSourcesModel = new UserLinkedSources();
32 11
        $this->userModel = new Users();
33
34 11
        if (!isset($this->config->jwt)) {
35
            throw new ServerErrorHttpException('You need to configure your app JWT');
36
        }
37 11
    }
38
39
    /**
40
    * Set the email config array we are going to be sending
41
    *
42
    * @param String $emailAction
43
    * @param Users  $user
44
    */
45 1
    protected function sendEmail(BakaUsers $user, string $type): void
46
    {
47 1
        $send = true;
48 1
        $subject = null;
49 1
        $body = null;
50
51 1
        switch ($type) {
52
            case 'recover':
53
                $recoveryLink = $this->config->app->frontEndUrl . '/user/reset/' . $user->user_activation_forgot;
54
55
                $subject = _('Password Recovery');
56
                $body = sprintf(_('Click %shere%s to set a new password for your account.'), '<a href="' . $recoveryLink . '" target="_blank">', '</a>');
57
58
                // send email to recover password
59
                break;
60
            case 'reset':
61
                $activationUrl = $this->config->app->frontEndUrl . '/user/activate/' . $user->user_activation_key;
62
63
                $subject = _('Password Updated!');
64
                $body = sprintf(_('Your password was update please, use this link to activate your account: %sActivate account%s'), '<a href="' . $activationUrl . '">', '</a>');
65
                // send email that password was update
66
                break;
67
            default:
68 1
                $send = false;
69 1
                break;
70
        }
71
72 1
        if ($send) {
73
            $this->mail
74
                ->to($user->email)
75
                ->subject($subject)
76
                ->content($body)
77
                ->sendNow();
78
        }
79 1
    }
80
}
81