Completed
Push — develop ( 5abd4c...dae2aa )
by Abdelrahman
09:07
created

PasswordResetController::process()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Illuminate\Support\Str;
8
use Rinvex\Auth\Contracts\PasswordResetBrokerContract;
9
use Cortex\Foundation\Http\Controllers\AbstractController;
10
use Cortex\Auth\Http\Requests\Adminarea\PasswordResetRequest;
11
use Cortex\Auth\Http\Requests\Adminarea\PasswordResetSendRequest;
12
use Cortex\Auth\Http\Requests\Adminarea\PasswordResetProcessRequest;
13
use Cortex\Auth\Http\Requests\Adminarea\PasswordResetPostProcessRequest;
14
15
class PasswordResetController extends AbstractController
16
{
17
    /**
18
     * Show the password reset request form.
19
     *
20
     * @param Cortex\Auth\Http\Requests\Adminarea\PasswordResetRequest
21
     *
22
     * @return \Illuminate\View\View
23
     */
24
    public function request(PasswordResetRequest $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        return view('cortex/auth::adminarea.pages.passwordreset-request');
27
    }
28
29
    /**
30
     * Process the password reset request form.
31
     *
32
     * @param \Cortex\Auth\Http\Requests\Adminarea\PasswordResetSendRequest $request
33
     *
34
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
35
     */
36
    public function send(PasswordResetSendRequest $request)
37
    {
38
        $result = app('auth.password')
39
            ->broker($this->getBroker())
40
            ->sendResetLink($request->only(['email']));
41
42
        switch ($result) {
43
            case PasswordResetBrokerContract::RESET_LINK_SENT:
44
                return intend([
45
                    'url' => route('adminarea.home'),
46
                    'with' => ['success' => trans($result)],
47
                ]);
48
49
            case PasswordResetBrokerContract::INVALID_USER:
50
            default:
51
                return intend([
52
                    'back' => true,
53
                    'withInput' => $request->only(['email']),
54
                    'withErrors' => ['email' => trans($result)],
55
                ]);
56
        }
57
    }
58
59
    /**
60
     * Show the password reset form.
61
     *
62
     * @param \Cortex\Auth\Http\Requests\Adminarea\PasswordResetProcessRequest $request
63
     *
64
     * @return \Illuminate\View\View
65
     */
66
    public function reset(PasswordResetProcessRequest $request)
67
    {
68
        $credentials = $request->only('email', 'expiration', 'token');
69
70
        return view('cortex/auth::adminarea.pages.passwordreset')->with($credentials);
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
71
    }
72
73
    /**
74
     * Process the password reset form.
75
     *
76
     * @param \Cortex\Auth\Http\Requests\Adminarea\PasswordResetPostProcessRequest $request
77
     *
78
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
79
     */
80
    public function process(PasswordResetPostProcessRequest $request)
81
    {
82
        $result = app('auth.password')
83
            ->broker($this->getBroker())
84
            ->reset($request->only(['email', 'expiration', 'token', 'password', 'password_confirmation']), function ($user, $password) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
85
                $user->fill([
86
                    'password' => $password,
87
                    'remember_token' => Str::random(60),
88
                ])->forceSave();
89
            });
90
91
        switch ($result) {
92
            case PasswordResetBrokerContract::PASSWORD_RESET:
93
                return intend([
94
                    'url' => route('adminarea.login'),
95
                    'with' => ['success' => trans($result)],
96
                ]);
97
98
            case PasswordResetBrokerContract::INVALID_USER:
99
            case PasswordResetBrokerContract::INVALID_TOKEN:
100
            case PasswordResetBrokerContract::EXPIRED_TOKEN:
101
            case PasswordResetBrokerContract::INVALID_PASSWORD:
102
            default:
103
                return intend([
104
                    'back' => true,
105
                    'withInput' => $request->only(['email']),
106
                    'withErrors' => ['email' => trans($result)],
107
                ]);
108
        }
109
    }
110
}
111