Completed
Push — develop ( 676a40...1f1713 )
by Abdelrahman
02:04
created

EmailVerificationController::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Controllers\Frontend;
6
7
use Carbon\Carbon;
8
use Cortex\Foundation\Http\Controllers\AbstractController;
9
use Rinvex\Fort\Contracts\EmailVerificationBrokerContract;
10
use Cortex\Fort\Http\Requests\Frontend\EmailVerificationRequest;
11
use Cortex\Fort\Http\Requests\Frontend\EmailVerificationProcessRequest;
12
13
class EmailVerificationController extends AbstractController
14
{
15
    /**
16
     * Show the email verification request form.
17
     *
18
     * @param \Cortex\Fort\Http\Requests\Frontend\EmailVerificationRequest $request
19
     *
20
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
21
     */
22
    public function request(EmailVerificationRequest $request)
23
    {
24
        return view('cortex/fort::frontend.verification.email-request');
25
    }
26
27
    /**
28
     * Process the email verification request form.
29
     *
30
     * @param \Cortex\Fort\Http\Requests\Frontend\EmailVerificationProcessRequest $request
31
     *
32
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
33
     */
34
    public function send(EmailVerificationProcessRequest $request)
35
    {
36
        $result = app('rinvex.fort.emailverification')
37
            ->broker($this->getBroker())
38
            ->sendVerificationLink($request->only(['email']));
39
40
        switch ($result) {
41
            case EmailVerificationBrokerContract::LINK_SENT:
42
                return intend([
43
                    'url' => '/',
44
                    'with' => ['success' => trans('cortex/fort::'.$result)],
45
                ]);
46
47
            default:
48
                return intend([
49
                    'back' => true,
50
                    'withInput' => $request->only(['email']),
51
                    'withErrors' => ['email' => trans('cortex/fort::'.$result)],
52
                ]);
53
        }
54
    }
55
56
    /**
57
     * Process the email verification.
58
     *
59
     * @param \Cortex\Fort\Http\Requests\Frontend\EmailVerificationProcessRequest $request
60
     *
61
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
62
     */
63
    public function verify(EmailVerificationProcessRequest $request)
64
    {
65
        $result = app('rinvex.fort.emailverification')
66
            ->broker($this->getBroker())
67
            ->verify($request->only(['email', 'token']), function ($user) {
68
                $user->fill([
69
                    'email_verified' => true,
70
                    'email_verified_at' => new Carbon(),
71
                ])->forceSave();
72
            });
73
74
        switch ($result) {
75
            case EmailVerificationBrokerContract::EMAIL_VERIFIED:
76
                return intend([
77
                    'url' => $request->user($this->getGuard()) ? route('frontend.account.settings') : route('frontend.auth.login'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
78
                    'with' => ['success' => trans('cortex/fort::'.$result)],
79
                ]);
80
81
            case EmailVerificationBrokerContract::INVALID_USER:
82
            case EmailVerificationBrokerContract::INVALID_TOKEN:
83
            default:
84
                return intend([
85
                    'url' => route('frontend.verification.email.request'),
86
                    'withInput' => $request->only(['email']),
87
                    'withErrors' => ['email' => trans('cortex/fort::'.$result)],
88
                ]);
89
        }
90
    }
91
}
92