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

ActivationController::redirectPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
crap 12
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