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

ActivationController::requestActivationCode()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.9256
Metric Value
dl 0
loc 18
ccs 6
cts 9
cp 0.6667
rs 8.8571
cc 5
eloc 9
nc 4
nop 1
crap 5.9256
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