Passed
Push — master ( 7a2ae7...77adf5 )
by Curtis
12:45 queued 06:15
created

VerificationController::validator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\Providers\RouteServiceProvider;
7
use Illuminate\Foundation\Auth\VerifiesEmails;
8
use Illuminate\Http\Request;
9
use LaravelEnso\Core\App\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...
10
use Illuminate\Support\Facades\Validator;
11
use App\Models\User;
12
use App\Activation;
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
        $data = $request->all();
40
        $this->validator($data)->validate();
41
        try{
42
            $token = $request->get('token');
43
            $activation = Activation::where('token', $token)->first();
44
            if($activation === null) {
45
                return response()->json(
46
                    [
47
                        'error' =>
48
                                [
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
                                [
62
                                    'code' => 301,
63
                                    'message' => 'There is not such user.'
64
                                ],
65
                    ],
66
                    Response::HTTP_UNPROCESSABLE_ENTITY);
67
            }
68
            $user->is_active = 1;
69
            $user->email_verified_at = date('Y-m-d H:i:s');
70
            $user->save();
71
            Activation::where('user_id', $user_id)->delete();
72
            return response()->json([
73
                'csrfToken' => csrf_token(),
74
            ]);
75
        }catch(\Exception $e) {
76
            throw $e;
77
        }
78
    }
79
}
80