Completed
Push — master ( 27a761...fea4e1 )
by Abdelrahman
01:44 queued 10s
created

PasswordResetController::process()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
cc 5
nc 5
nop 1
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) {
84
                $user->fill([
85
                    'password' => $password,
86
                    'remember_token' => str_random(60),
0 ignored issues
show
Deprecated Code introduced by
The function str_random() has been deprecated with message: Str::random() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
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
            default:
101
                return intend([
102
                    'back' => true,
103
                    'withInput' => $request->only(['email']),
104
                    'withErrors' => ['email' => trans($result)],
105
                ]);
106
        }
107
    }
108
}
109