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

PhoneVerificationController::process()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Adminarea;
6
7
use Cortex\Auth\Traits\TwoFactorAuthenticatesUsers;
8
use Cortex\Foundation\Http\Controllers\AbstractController;
9
use Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationRequest;
10
use Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationSendRequest;
11
use Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationVerifyRequest;
12
use Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationProcessRequest;
13
14
class PhoneVerificationController extends AbstractController
15
{
16
    use TwoFactorAuthenticatesUsers;
17
18
    /**
19
     * Show the phone verification form.
20
     *
21
     * @param \Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationRequest $request
22
     *
23
     * @return \Illuminate\View\View
24
     */
25
    public function request(PhoneVerificationRequest $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...
26
    {
27
        return view('cortex/auth::adminarea.pages.verification-phone-request');
28
    }
29
30
    /**
31
     * Process the phone verification request form.
32
     *
33
     * @param \Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationSendRequest $request
34
     *
35
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
36
     */
37
    public function send(PhoneVerificationSendRequest $request)
38
    {
39
        $user = $request->user($this->getGuard())
40
                ?? $request->attemptUser($this->getGuard())
0 ignored issues
show
Documentation Bug introduced by
The method attemptUser does not exist on object<Cortex\Auth\Http\...erificationSendRequest>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
41
                   ?? app('cortex.auth.admin')->whereNotNull('phone')->where('phone', $request->input('phone'))->first();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 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...
42
43
        $user->sendPhoneVerificationNotification($request->get('method'), true);
44
45
        return intend([
46
            'url' => route('adminarea.verification.phone.verify', ['phone' => $user->phone]),
47
            'with' => ['success' => trans('cortex/auth::messages.verification.phone.sent')],
48
        ]);
49
    }
50
51
    /**
52
     * Show the phone verification form.
53
     *
54
     * @param \Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationVerifyRequest $request
55
     *
56
     * @return \Illuminate\View\View
57
     */
58
    public function verify(PhoneVerificationVerifyRequest $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...
59
    {
60
        return view('cortex/auth::adminarea.pages.verification-phone-token');
61
    }
62
63
    /**
64
     * Process the phone verification form.
65
     *
66
     * @param \Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationProcessRequest $request
67
     *
68
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
69
     */
70
    public function process(PhoneVerificationProcessRequest $request)
71
    {
72
        // Guest trying to authenticate through TwoFactor
73
        if (($attemptUser = $request->attemptUser($this->getGuard())) && $this->attemptTwoFactor($attemptUser, $request->get('token'))) {
0 ignored issues
show
Documentation Bug introduced by
The method attemptUser does not exist on object<Cortex\Auth\Http\...ficationProcessRequest>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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...
74
            auth()->guard($this->getGuard())->login($attemptUser, $request->session()->get('cortex.auth.twofactor.remember'));
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 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...
75
            $request->session()->forget('cortex.auth.twofactor'); // @TODO: Do we need to forget session, or it's already gone after login?
0 ignored issues
show
Bug introduced by
The method forget() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 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...
76
77
            return intend([
78
                'intended' => route('adminarea.home'),
79
                'with' => ['success' => trans('cortex/auth::messages.auth.login')],
80
            ]);
81
        }
82
83
        // Logged in user OR A GUEST trying to verify phone
84
        if (($user = $request->user($this->getGuard()) ?? app('cortex.auth.admin')->whereNotNull('phone')->where('phone', $request->get('phone'))->first()) && $this->isValidTwoFactorPhone($user, $request->get('token'))) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 221 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
            // Profile update
86
            $user->fill([
87
                'phone_verified' => true,
88
                'phone_verified_at' => now(),
89
            ])->forceSave();
90
91
            return intend([
92
                'url' => route('adminarea.account.settings'),
93
                'with' => ['success' => trans('cortex/auth::messages.verification.phone.verified')],
94
            ]);
95
        }
96
97
        return intend([
98
            'back' => true,
99
            'withErrors' => ['token' => trans('cortex/auth::messages.verification.twofactor.invalid_token')],
100
        ]);
101
    }
102
}
103