Completed
Push — master ( e5351e...648c39 )
by Julien
15:06
created

BuildersController::disconnect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
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
            return response()->json(['data' => $user, 'state' => true]);
74
75
        } else {
76
            return response()->json(['data' => "Invalid parameters", 'state' => false]);
77
        }
78
79
        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...
80
    }
81
82
    /**
83
     * Auth
84
     */
85
    public function disconnect()
86
    {
87
        if (auth()->guard('api')->check()) {
88
89
            auth()->guard('api')->logout();
90
91
            return response()->json(['state' => true]);
92
93
        } else {
94
95
            return response()->json(['state' => false]);
96
        }
97
98
    }
99
    /**
100
     * Auth
101
     */
102
    public function connectAlreadyExist(Request $request)
103
    {
104
        $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...
105
        $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...
106
107
        if (!empty($email) && !empty($password)) {
108
                $auth = auth()->guard('user');
109
110
                $credentials = [
111
                    'email' =>  $email,
112
                    'password' =>  $password,
113
                    'enabled' => true
114
                ];
115
116
                if ($auth->attempt($credentials) && auth()->guard('user')->check()) {
117
118
                    return response()->json(['data' => auth()->guard('user')->user()->toArray(), 'state' => true]);
119
                } else {
120
                    return response()->json(['data' => "Bad email or password", 'state' => false]);
121
                }
122
123
        } else {
124
            return response()->json(['data' => "Invalid parameters", 'state' => false]);
125
        }
126
127
        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...
128
    }
129
130
    /**
131
     * List account
132
     */
133
    public function listAccount()
134
    {
135
        $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
136
        $collection = new \MongoDB\Collection($manager, 'builders', 'account');
137
138
        $result = $collection->find()->toArray();
139
140
        $tab = [];
141
        foreach($result as $one){
142
            $tab[] = $one->bsonSerialize();
143
        }
144
145
        return response()->json($tab);
146
    }
147
148
    /**
149
     * Update account
150
     */
151 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...
152
    {
153
        $data = [
154
          "name" => $request->name,
155
          "cp" => $request->cp,
156
          "phone" => $request->phone,
157
          "sexe" => $request->sexe,
158
          "news" => $request->news,
159
        ];
160
161
        $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
162
        $collection = new \MongoDB\Collection($manager, 'builders', 'account');
163
        $stat = [
164
            'email'    => $request->email,
165
            'data'    => $data,
166
            'created' => new  \DateTime("now"),
167
        ];
168
169
        try{
170
            $collection->updateOne(["email" => $request->email], $stat);
171
        }catch (\Exception $e){
172
            return response()->json(['state' => false]);
173
        }
174
175
        $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...
176
        return response()->json(['data' => $data, 'state' => true]);
177
    }
178
179
}
180