Completed
Push — master ( b1c4b6...a05019 )
by Şəhriyar
19s
created

ActivationController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 75.86%

Importance

Changes 5
Bugs 2 Features 3
Metric Value
wmc 16
c 5
b 2
f 3
lcom 1
cbo 10
dl 0
loc 81
ccs 22
cts 29
cp 0.7586
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A requestActivationCode() 0 17 4
B activate() 0 19 8
A redirectPath() 0 8 3
1
<?php namespace App\Http\Controllers\Users;
2
3
use App\Contracts\Registrar;
4
use App\Events\Users\RequestedActivationLink;
5
use App\Exceptions\Common\NotImplementedException;
6
use App\Exceptions\Users\UserAlreadyActivatedException;
7
use App\Exceptions\Users\UserNotFoundException;
8
use App\Http\Controllers\Controller;
9
use App\Models\User;
10
use App\Traits\Users\Activates;
11
use Illuminate\Http\Request;
12
13
class ActivationController extends Controller
14
{
15
    use Activates;
16
17
    /**
18
     * Create a new authentication controller instance.
19
     */
20 5
    public function __construct()
21
    {
22 5
        $this->middleware('auth', ['only' => ['requestActivationCode']]);
23 5
    }
24
25
    /**
26
     * Request account activation link via email.
27
     *
28
     * @param \Illuminate\Http\Request $request
29
     *
30
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
31
     * @throws \App\Exceptions\Users\UserAlreadyActivatedException
32
     */
33 2
    public function requestActivationCode(Request $request)
34
    {
35
        /** @var User $user */
36 2
        $user = app('auth.driver')->user();
37
38 2
        if (false !== $this->completed($user)) {
39 1
            throw new UserAlreadyActivatedException;
40
        }
41
42 1
        app('events')->fire(new RequestedActivationLink($user));
43
44 1
        if ($request->ajax() || $request->wantsJson()) {
45 1
            return response()->json(['message' => 'Activation link sent']);
46
        }
47
48
        return redirect()->back()->with('message', 'Activation link sent');
49
    }
50
51
    /**
52
     * Activate an account [Web only].
53
     *
54
     * @param \Illuminate\Http\Request $request
55
     * @param \App\Contracts\Registrar $registrar
56
     * @param string|null              $token
57
     *
58
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse
59
     */
60 3
    public function activate(Request $request, Registrar $registrar, $token = null)
61 2
    {
62 3
        switch ($requestMethod = $request->getMethod()) {
63 3
            case 'GET':
64 1
                if ($request->ajax() || $request->wantsJson()) {
65 1
                    throw new NotImplementedException;
66
                }
67
                break;
68 2
            case 'POST':
69 2
                if (!$request->ajax() && !$request->wantsJson()) {
70
                    throw new NotImplementedException;
71
                }
72 2
                break;
73 2
        }
74
75 2
        $registrar->activate($token);
76
77 1
        return $requestMethod == 'GET' ? redirect($this->redirectPath())->with('message', 'Activation successful') : response()->json(['message' => 'Activated']);
78
    }
79
80
    /**
81
     * Get the post register / login redirect path.
82
     *
83
     * @return string
84
     */
85
    private function redirectPath()
86
    {
87
        if (isset($this->redirectPath)) {
88
            return $this->redirectPath;
0 ignored issues
show
Bug introduced by
The property redirectPath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
89
        }
90
91
        return isset($this->redirectTo) ? $this->redirectTo : '/';
0 ignored issues
show
Bug introduced by
The property redirectTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
92
    }
93
}
94