Completed
Push — master ( 94434d...92e1c0 )
by Şəhriyar
116:27 queued 99:47
created

ActivationController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 68.97%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 17
c 4
b 1
f 2
lcom 1
cbo 10
dl 0
loc 80
ccs 20
cts 29
cp 0.6897
rs 10

4 Methods

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