Completed
Push — master ( 648c39...33cf5d )
by Julien
14:10
created

BuildersController::getAnnouncesCart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
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
use LukePOLO\LaraCart\Facades\LaraCart;
9
10
11
/**
12
 * Class BuildersController.
13
 */
14
class BuildersController extends Controller
15
{
16
17
    /**
18
     * Create account
19
     */
20 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...
21
    {
22
        $data = [
23
          "name" => $request->name,
24
          "cp" => $request->cp,
25
          "phone" => $request->phone,
26
          "sexe" => $request->sexe,
27
          "news" => $request->news,
28
        ];
29
30
        $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
31
        $collection = new \MongoDB\Collection($manager, 'builders', 'account');
32
        $stat = [
33
            'email'    => $request->email,
34
            'data'    => $data,
35
            'created' => new  \DateTime("now"),
36
        ];
37
38
        try{
39
            $collection->insertOne($stat);
40
        }catch (\Exception $e){
41
            return response()->json(['state' => false]);
42
        }
43
44
        $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...
45
        return response()->json(['data' => $data, 'state' => true]);
46
    }
47
48
    /**
49
     * Auth
50
     */
51
    public function connect(Request $request)
52
    {
53
        $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...
54
        $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...
55
56
        if (!empty($email) && !empty($password)) {
57
            $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
58
            $collection = new \MongoDB\Collection($manager, 'builders', 'account');
59
60
61
            if ($collection->count(["email" => $email]) == 0) {
62
                return response()->json(['data' => "User doesn't exist", 'state' => false]);
63
            }
64
65
            $user = $collection->findOne(["email" => $email])->bsonSerialize();
66
67
            if(password_verify($password, $user->password) == false){
68
                return response()->json(['data' => "Bad email or password", 'state' => false]);
69
            }
70
71
            $api = new Api($user);
72
            Auth::login($api);
73
74
            return response()->json(['data' => $user, 'state' => true]);
75
76
        } else {
77
            return response()->json(['data' => "Invalid parameters", 'state' => false]);
78
        }
79
80
        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...
81
    }
82
83
    /**
84
     * Auth
85
     */
86
    public function disconnect()
87
    {
88
        if (auth()->guard('api')->check()) {
89
90
            Auth::logout();
91
92
            return response()->json(['state' => true]);
93
94
        } else {
95
96
            return response()->json(['state' => false]);
97
        }
98
99
    }
100
101
102
    /**
103
     * Auth
104
     */
105
    public function getAnnouncesCart()
106
    {
107
108
        $items = LaraCart::getItems();
109
110
        return response()->json(['data' => $items, 'state' => true]);
111
    }
112
113
114
115
    /**
116
     * Auth
117
     */
118
    public function addAnnounceCart(Request $request)
119
    {
120
        $id = $request->id;
1 ignored issue
show
Bug introduced by
The property id 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...
121
        $title = $request->title;
1 ignored issue
show
Bug introduced by
The property title 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...
122
        $quantity = $request->quantity;
1 ignored issue
show
Bug introduced by
The property quantity 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...
123
        $prix = $request->prix;
1 ignored issue
show
Bug introduced by
The property prix 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...
124
        $options = $request->options;
1 ignored issue
show
Bug introduced by
The property options 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...
125
126
        $item = LaraCart::add($id, $title, $quantity, $prix, ['size' => $options ]);
127
128
        return response()->json(['data' => $item, 'state' => true]);
129
    }
130
131
132
    /**
133
     * Auth
134
     */
135
    public function removeAnnounceCart(Request $request)
136
    {
137
        $hash = $request->hash;
1 ignored issue
show
Bug introduced by
The property hash 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...
138
139
        LaraCart::removeItem($hash);
140
141
        $items = LaraCart::getItems();
142
143
        return response()->json(['data' => $items, 'state' => true]);
144
    }
145
146
147
    /**
148
     * Auth
149
     */
150
    public function getTotalAnnouncesCart()
151
    {
152
        $data['subtotal'] = LaraCart::subTotal($tax = false, $format = true, $withDiscount = true);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
153
        $data['totaldiscount'] = LaraCart::totalDiscount($formatted = false);
154
        $data['taxtotal'] = LaraCart::taxTotal($formatted = false);
155
        $data['total'] = LaraCart::total($formatted = false, $withDiscount = true);
156
157
        return response()->json(['data' => $data, 'state' => true]);
158
    }
159
160
161
    /**
162
     * Auth
163
     */
164
    public function connectAlreadyExist(Request $request)
165
    {
166
        $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...
167
        $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...
168
169
        if (!empty($email) && !empty($password)) {
170
                $auth = auth()->guard('user');
171
172
                $credentials = [
173
                    'email' =>  $email,
174
                    'password' =>  $password,
175
                    'enabled' => true
176
                ];
177
178
                if ($auth->attempt($credentials) && auth()->guard('user')->check()) {
179
180
                    return response()->json(['data' => auth()->guard('user')->user()->toArray(), 'state' => true]);
181
                } else {
182
                    return response()->json(['data' => "Bad email or password", 'state' => false]);
183
                }
184
185
        } else {
186
            return response()->json(['data' => "Invalid parameters", 'state' => false]);
187
        }
188
189
        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...
190
    }
191
192
    /**
193
     * List account
194
     */
195
    public function listAccount()
196
    {
197
        $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
198
        $collection = new \MongoDB\Collection($manager, 'builders', 'account');
199
200
        $result = $collection->find()->toArray();
201
202
        $tab = [];
203
        foreach($result as $one){
204
            $tab[] = $one->bsonSerialize();
205
        }
206
207
        return response()->json($tab);
208
    }
209
210
211
212
    /**
213
     * Update account
214
     */
215 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...
216
    {
217
        $data = [
218
          "name" => $request->name,
219
          "cp" => $request->cp,
220
          "phone" => $request->phone,
221
          "sexe" => $request->sexe,
222
          "news" => $request->news,
223
        ];
224
225
        $manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
226
        $collection = new \MongoDB\Collection($manager, 'builders', 'account');
227
        $stat = [
228
            'email'    => $request->email,
229
            'data'    => $data,
230
            'created' => new  \DateTime("now"),
231
        ];
232
233
        try{
234
            $collection->updateOne(["email" => $request->email], $stat);
235
        }catch (\Exception $e){
236
            return response()->json(['state' => false]);
237
        }
238
239
        $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...
240
        return response()->json(['data' => $data, 'state' => true]);
241
    }
242
243
}
244