Completed
Push — master ( 20a6e3...887da8 )
by Abdelrahman
04:09 queued 02:12
created

PasswordResetController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 4 1
A send() 0 22 3
A reset() 0 6 1
B process() 0 30 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Rinvex\Auth\Contracts\PasswordResetBrokerContract;
8
use Cortex\Foundation\Http\Controllers\AbstractController;
9
use Cortex\Auth\Http\Requests\Adminarea\PasswordResetRequest;
10
use Cortex\Auth\Http\Requests\Adminarea\PasswordResetSendRequest;
11
use Cortex\Auth\Http\Requests\Adminarea\PasswordResetProcessRequest;
12
use Cortex\Auth\Http\Requests\Adminarea\PasswordResetPostProcessRequest;
13
14
class PasswordResetController extends AbstractController
15
{
16
    /**
17
     * Show the password reset request form.
18
     *
19
     * @param Cortex\Auth\Http\Requests\Adminarea\PasswordResetRequest
20
     *
21
     * @return \Illuminate\View\View
22
     */
23
    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...
24
    {
25
        return view('cortex/auth::adminarea.pages.passwordreset-request');
26
    }
27
28
    /**
29
     * Process the password reset request form.
30
     *
31
     * @param \Cortex\Auth\Http\Requests\Adminarea\PasswordResetSendRequest $request
32
     *
33
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
34
     */
35
    public function send(PasswordResetSendRequest $request)
36
    {
37
        $result = app('auth.password')
38
            ->broker($this->getPasswordResetBroker())
39
            ->sendResetLink($request->only(['email']));
40
41
        switch ($result) {
42
            case PasswordResetBrokerContract::RESET_LINK_SENT:
43
                return intend([
44
                    'url' => route('adminarea.home'),
45
                    'with' => ['success' => trans($result)],
46
                ]);
47
48
            case PasswordResetBrokerContract::INVALID_USER:
49
            default:
50
                return intend([
51
                    'back' => true,
52
                    'withInput' => $request->only(['email']),
53
                    'withErrors' => ['email' => trans($result)],
54
                ]);
55
        }
56
    }
57
58
    /**
59
     * Show the password reset form.
60
     *
61
     * @param \Cortex\Auth\Http\Requests\Adminarea\PasswordResetProcessRequest $request
62
     *
63
     * @return \Illuminate\View\View
64
     */
65
    public function reset(PasswordResetProcessRequest $request)
66
    {
67
        $credentials = $request->only('email', 'expiration', 'token');
68
69
        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...
70
    }
71
72
    /**
73
     * Process the password reset form.
74
     *
75
     * @param \Cortex\Auth\Http\Requests\Adminarea\PasswordResetPostProcessRequest $request
76
     *
77
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
78
     */
79
    public function process(PasswordResetPostProcessRequest $request)
80
    {
81
        $result = app('auth.password')
82
            ->broker($this->getPasswordResetBroker())
83
            ->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...
84
                $user->fill([
85
                    'password' => $password,
86
                    'remember_token' => str_random(60),
87
                ])->forceSave();
88
            });
89
90
        switch ($result) {
91
            case PasswordResetBrokerContract::PASSWORD_RESET:
92
                return intend([
93
                    'url' => route('adminarea.login'),
94
                    'with' => ['success' => trans($result)],
95
                ]);
96
97
            case PasswordResetBrokerContract::INVALID_USER:
98
            case PasswordResetBrokerContract::INVALID_TOKEN:
99
            case PasswordResetBrokerContract::EXPIRED_TOKEN:
100
            case PasswordResetBrokerContract::INVALID_PASSWORD:
101
            default:
102
                return intend([
103
                    'back' => true,
104
                    'withInput' => $request->only(['email']),
105
                    'withErrors' => ['email' => trans($result)],
106
                ]);
107
        }
108
    }
109
}
110