PhoneVerificationController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 88
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 4 1
A send() 0 13 1
A verify() 0 4 1
A process() 0 31 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Controllers\Frontarea;
6
7
use Cortex\Auth\Traits\TwoFactorAuthenticatesUsers;
8
use Cortex\Foundation\Http\Controllers\AbstractController;
9
use Cortex\Auth\Http\Requests\Frontarea\PhoneVerificationRequest;
10
use Cortex\Auth\Http\Requests\Frontarea\PhoneVerificationSendRequest;
11
use Cortex\Auth\Http\Requests\Frontarea\PhoneVerificationVerifyRequest;
12
use Cortex\Auth\Http\Requests\Frontarea\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\Frontarea\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::frontarea.pages.verification-phone-request');
28
    }
29
30
    /**
31
     * Process the phone verification request form.
32
     *
33
     * @param \Cortex\Auth\Http\Requests\Frontarea\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.member')->whereNotNull('phone')->where('phone', $request->input('phone'))->first();
42
43
        $user->sendPhoneVerificationNotification($request->get('method'), true);
44
45
        return intend([
46
            'url' => route('frontarea.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\Frontarea\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::frontarea.pages.verification-phone-token');
61
    }
62
63
    /**
64
     * Process the phone verification form.
65
     *
66
     * @param \Cortex\Auth\Http\Requests\Frontarea\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...
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...
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...
76
77
            return intend([
78
                'intended' => route('frontarea.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.member')->whereNotNull('phone')->where('phone', $request->get('phone'))->first()) && $this->isValidTwoFactorPhone($user, $request->get('token'))) {
85
            // Profile update
86
            $user->fill([
87
                'phone_verified_at' => now(),
88
            ])->forceSave();
89
90
            return intend([
91
                'url' => route('frontarea.account.settings'),
92
                'with' => ['success' => trans('cortex/auth::messages.verification.phone.verified')],
93
            ]);
94
        }
95
96
        return intend([
97
            'back' => true,
98
            'withErrors' => ['token' => trans('cortex/auth::messages.verification.twofactor.invalid_token')],
99
        ]);
100
    }
101
}
102