Completed
Push — master ( 5d14d5...e5351e )
by Julien
09:39
created

BuildersController::connect()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 33
rs 8.439
cc 5
eloc 18
nc 4
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
use App\Http\Models\Api;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Input;
8
9
10
/**
11
 * Class BuildersController.
12
 */
13
class BuildersController extends Controller
14
{
15
16
    /**
17
     * Create account
18
     */
19 View Code Duplication
    public function createAccount(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        $data = [
22
          "name" => $request->name,
23
          "cp" => $request->cp,
24
          "phone" => $request->phone,
25
          "sexe" => $request->sexe,
26
          "news" => $request->news,
27
        ];
28
29
        $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
30
        $collection = new \MongoDB\Collection($manager, 'builders', 'account');
31
        $stat = [
32
            'email'    => $request->email,
33
            'data'    => $data,
34
            'created' => new  \DateTime("now"),
35
        ];
36
37
        try{
38
            $collection->insertOne($stat);
39
        }catch (\Exception $e){
40
            return response()->json(['state' => false]);
41
        }
42
43
        $data["email"] = $request->email;
1 ignored issue
show
Bug introduced by
The property email does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
44
        return response()->json(['data' => $data, 'state' => true]);
45
    }
46
47
    /**
48
     * Auth
49
     */
50
    public function connect(Request $request)
51
    {
52
        $email = $request->email;
1 ignored issue
show
Bug introduced by
The property email does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
        $password = $request->password;
1 ignored issue
show
Bug introduced by
The property password does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
55
        if (!empty($email) && !empty($password)) {
56
            $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
57
            $collection = new \MongoDB\Collection($manager, 'builders', 'account');
58
59
60
            if ($collection->count(["email" => $email]) == 0) {
61
                return response()->json(['data' => "User doesn't exist", 'state' => false]);
62
            }
63
64
            $user = $collection->findOne(["email" => $email])->bsonSerialize();
65
66
            if(password_verify($password, $user->password) == false){
67
                return response()->json(['data' => "Bad email or password", 'state' => false]);
68
            }
69
70
            $api = new Api($user);
71
            Auth::login($api);
72
73
            dump(Auth::user());
74
75
            return response()->json(['data' => $user, 'state' => true]);
76
77
        } else {
78
            return response()->json(['data' => "Invalid parameters", 'state' => false]);
79
        }
80
81
        return response()->json(['data' => "Bad credentials", 'state' => false]);
0 ignored issues
show
Unused Code introduced by
return response()->json(...s', 'state' => false)); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
82
    }
83
    /**
84
     * Auth
85
     */
86
    public function connectAlreadyExist(Request $request)
87
    {
88
        $email = $request->email;
1 ignored issue
show
Bug introduced by
The property email does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
89
        $password = $request->password;
1 ignored issue
show
Bug introduced by
The property password does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
90
91
        if (!empty($email) && !empty($password)) {
92
                $auth = auth()->guard('user');
93
94
                $credentials = [
95
                    'email' =>  $email,
96
                    'password' =>  $password,
97
                    'enabled' => true
98
                ];
99
100
                if ($auth->attempt($credentials) && auth()->guard('user')->check()) {
101
102
                    return response()->json(['data' => auth()->guard('user')->user()->toArray(), 'state' => true]);
103
                } else {
104
                    return response()->json(['data' => "Bad email or password", 'state' => false]);
105
                }
106
107
        } else {
108
            return response()->json(['data' => "Invalid parameters", 'state' => false]);
109
        }
110
111
        return response()->json(['data' => "Bad credentials", 'state' => false]);
0 ignored issues
show
Unused Code introduced by
return response()->json(...s', 'state' => false)); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
112
    }
113
114
    /**
115
     * List account
116
     */
117
    public function listAccount()
118
    {
119
        $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
120
        $collection = new \MongoDB\Collection($manager, 'builders', 'account');
121
122
        $result = $collection->find()->toArray();
123
124
        $tab = [];
125
        foreach($result as $one){
126
            $tab[] = $one->bsonSerialize();
127
        }
128
129
        return response()->json($tab);
130
    }
131
132
    /**
133
     * Update account
134
     */
135 View Code Duplication
    public function updateAccount(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $data = [
138
          "name" => $request->name,
139
          "cp" => $request->cp,
140
          "phone" => $request->phone,
141
          "sexe" => $request->sexe,
142
          "news" => $request->news,
143
        ];
144
145
        $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
146
        $collection = new \MongoDB\Collection($manager, 'builders', 'account');
147
        $stat = [
148
            'email'    => $request->email,
149
            'data'    => $data,
150
            'created' => new  \DateTime("now"),
151
        ];
152
153
        try{
154
            $collection->updateOne(["email" => $request->email], $stat);
155
        }catch (\Exception $e){
156
            return response()->json(['state' => false]);
157
        }
158
159
        $data["email"] = $request->email;
1 ignored issue
show
Bug introduced by
The property email does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
160
        return response()->json(['data' => $data, 'state' => true]);
161
    }
162
163
}
164