Completed
Pull Request — master (#45)
by Şəhriyar
11:38
created

ActivateController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 0
cbo 12
dl 0
loc 69
ccs 0
cts 33
cp 0
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A requestActivationCode() 0 17 3
B activate() 0 21 5
1
<?php namespace App\Http\Controllers\Auth;
2
3
use App\Events\Users\RequestedActivationLink;
4
use App\Exceptions\Common\ValidationException;
5
use App\Exceptions\Users\TokenNotValidException;
6
use App\Exceptions\Users\UserAlreadyActivatedException;
7
use App\Http\Controllers\Controller;
8
use App\Models\User;
9
use App\Models\UserActivation;
10
use App\Traits\Users\Activates;
11
use Illuminate\Foundation\Auth\RedirectsUsers;
12
use Illuminate\Http\Request;
13
14
class ActivateController extends Controller
15
{
16
    use Activates, RedirectsUsers;
17
18
    /**
19
     * Create a new authentication controller instance.
20
     */
21
    public function __construct()
22
    {
23
        $this->middleware('auth', ['only' => ['requestActivationCode']]);
24
    }
25
26
    /**
27
     * Request account activation link via email.
28
     *
29
     * @param \Illuminate\Http\Request $request
30
     *
31
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
32
     * @throws \App\Exceptions\Users\UserAlreadyActivatedException
33
     */
34
    public function requestActivationCode(Request $request)
35
    {
36
        /** @var User $user */
37
        $user = app('auth.driver')->user();
38
39
        if (false !== $this->completed($user)) {
40
            throw new UserAlreadyActivatedException;
41
        }
42
43
        app('events')->fire(new RequestedActivationLink($user));
44
45
        if ($request->expectsJson()) {
46
            return response()->json(['message' => 'Activation link sent']);
47
        }
48
49
        return redirect()->back()->with('message', 'Activation link sent');
50
    }
51
52
    /**
53
     * Activate an account [Web only].
54
     *
55
     * @param \Illuminate\Http\Request $request
56
     * @param string|null              $token
57
     *
58
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
59
     * @throws \App\Exceptions\Common\ValidationException
60
     */
61
    public function activate(Request $request, $token = null)
62
    {
63
        $data = !is_null($token) ? ['token' => $token] : $request->all();
64
        $validator = app('validator')->make($data, [
65
            'token' => 'required|string',
66
        ]);
67
        if ($validator->fails()) {
68
            throw new ValidationException($validator);
69
        }
70
71
        $activation = UserActivation::whereCode($data['token'])->first();
0 ignored issues
show
Bug introduced by
The method first does only exist in Illuminate\Database\Query\Builder, but not in App\Models\UserActivation.

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...
72
        if (!$activation) {
73
            throw new TokenNotValidException;
74
        }
75
        /** @var \App\Models\User $user */
76
        $user = User::findOrFail($activation->user_id);
77
78
        $this->complete($user, $data['token']);
79
80
        return ($request->expectsJson()) ? response()->json(['message' => 'Activated']) : redirect($this->redirectPath())->with('message', 'Activation successful');
81
    }
82
}
83