VerificationController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validator() 0 4 1
A verify_user() 0 39 4
A __construct() 0 2 1
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Activation;
6
use App\Http\Controllers\Controller;
7
use App\Models\User;
8
use App\Providers\RouteServiceProvider;
9
use Illuminate\Foundation\Auth\VerifiesEmails;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Validator;
12
use LaravelEnso\Core\Http\Controllers\Auth\LoginController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Http\Controllers\Auth\LoginController. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Symfony\Component\HttpFoundation\Response as Response;
14
15
class VerificationController extends Controller
16
{
17
    // use VerifiesEmails;
18
19
    protected $redirectTo = RouteServiceProvider::HOME;
20
21
    public function __construct()
22
    {
23
        // $this->middleware('auth');
24
        // $this->middleware('signed')->only('verify');
25
        // $this->middleware('throttle:6,1')->only('verify', 'resend');
26
    }
27
28
    protected function validator(array $data)
29
    {
30
        return Validator::make($data, [
31
            'token' => ['required', 'string', 'max:255'],
32
        ]);
33
    }
34
35
    /**
36
     * verify user token.
37
     */
38
    public function verify_user(Request $request)
39
    {
40
        $data = $request->all();
41
        $this->validator($data)->validate();
42
        try {
43
            $token = $request->get('token');
44
            $activation = Activation::where('token', $token)->first();
45
            if ($activation === null) {
46
                return response()->json(
47
                    [
48
                        'error' => [
49
                            'code' => 300,
50
                            'message' => 'Send activation code again.',
51
                        ],
52
                    ],
53
                    Response::HTTP_UNPROCESSABLE_ENTITY);
54
            }
55
            $user_id = $activation->user_id;
56
            $user = User::find($user_id);
57
            if ($user === null) {
58
                return response()->json(
59
                    [
60
                        'error' => [
61
                            'code' => 301,
62
                            'message' => 'There is not such user.',
63
                        ],
64
                    ],
65
                    Response::HTTP_UNPROCESSABLE_ENTITY);
66
            }
67
            $user->is_active = 1;
68
            $user->email_verified_at = date('Y-m-d H:i:s');
69
            $user->save();
70
            Activation::where('user_id', $user_id)->delete();
71
72
            return response()->json([
73
                'csrfToken' => csrf_token(),
74
            ]);
75
        } catch (\Exception $e) {
76
            throw $e;
77
        }
78
    }
79
}
80