Completed
Pull Request — master (#47)
by Şəhriyar
12:55
created

ActivateController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 12
dl 0
loc 62
ccs 0
cts 20
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A requestActivationCode() 0 17 3
B activate() 0 22 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
     * Request account activation link via email.
20
     *
21
     * @param \Illuminate\Http\Request $request
22
     *
23
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
24
     * @throws \App\Exceptions\Users\UserAlreadyActivatedException
25
     */
26
    public function requestActivationCode(Request $request)
27
    {
28
        /** @var User $user */
29
        $user = app('auth.driver')->user();
30
31
        if (false !== $this->completed($user)) {
32
            throw new UserAlreadyActivatedException;
33
        }
34
35
        app('events')->fire(new RequestedActivationLink($user));
36
37
        if ($request->expectsJson()) {
38
            return response()->json(['message' => 'Activation link sent']);
39
        }
40
41
        return redirect()->back()->with('message', 'Activation link sent');
42
    }
43
44
    /**
45
     * Activate an account [Web only].
46
     *
47
     * @param \Illuminate\Http\Request $request
48
     * @param string|null              $token
49
     *
50
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
51
     * @throws \App\Exceptions\Common\ValidationException
52
     */
53
    public function activate(Request $request, $token = null)
54
    {
55
        $data = !is_null($token) ? ['token' => $token] : $request->all();
56
        $validator = app('validator')->make($data, [
57
            'token' => 'required|string',
58
        ]);
59
        if ($validator->fails()) {
60
            throw new ValidationException($validator);
61
        }
62
63
        $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...
64
        if (!$activation) {
65
            throw new TokenNotValidException;
66
        }
67
68
        /** @var \App\Models\User $user */
69
        $user = User::findOrFail($activation->user_id);
70
71
        $this->complete($user, $data['token']);
72
73
        return ($request->expectsJson()) ? response()->json(['message' => 'Activated']) : redirect($this->redirectPath())->with('message', 'Activation successful');
74
    }
75
}
76