1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
use Illuminate\Http\Request; |
5
|
|
|
use Illuminate\Support\Facades\Input; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AdController. |
11
|
|
|
*/ |
12
|
|
|
class AdController extends Controller |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
public function adAnnounce(Request $request){ |
16
|
|
|
|
17
|
|
|
$data = [ |
18
|
|
|
"image" => Input::file('image'), |
19
|
|
|
"name" => $request->name, |
20
|
|
|
"email" => $request->email, |
21
|
|
|
"phone" => $request->phone, |
22
|
|
|
"description" => $request->description, |
23
|
|
|
"chambres" => $request->chambres, |
24
|
|
|
"pieces" => $request->pieces, |
25
|
|
|
"surface" => $request->surface, |
26
|
|
|
"prix" => $request->prix, |
27
|
|
|
"aid" => $request->aid |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
$file = null; |
|
|
|
|
31
|
|
|
$data["email"] = $request->email; |
|
|
|
|
32
|
|
|
|
33
|
|
|
$input = array('image' => Input::file('image')); |
34
|
|
|
|
35
|
|
|
$rules = array( |
36
|
|
|
'image' => 'image' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$validator = Validator::make($input, $rules); |
40
|
|
|
|
41
|
|
|
if ($validator->fails()) |
42
|
|
|
{ |
43
|
|
|
return response()->json(['data' => 'Fichier invalid', 'state' => false]); |
44
|
|
|
} else { |
45
|
|
|
if ($request->hasFile('image')) { |
46
|
|
|
|
47
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
48
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'ads'); |
49
|
|
|
$stat = [ |
50
|
|
|
'email' => $request->email, |
51
|
|
|
'data' => $data, |
52
|
|
|
'created' => new \DateTime("now"), |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
try{ |
56
|
|
|
$collection->insertOne($stat); |
57
|
|
|
} catch (\Exception $e){ |
58
|
|
|
return response()->json(['state' => false]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$file = $request->file('image'); |
62
|
|
|
$filename = $file->getClientOriginalName(); |
63
|
|
|
$destinationPath = public_path().'/uploads/ad'; |
64
|
|
|
$file->move($destinationPath, $filename); |
65
|
|
|
|
66
|
|
|
$data['image'] = asset($filename); |
67
|
|
|
return response()->json(['data' => $data, 'state' => true]); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* List ads |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function ads() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
83
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'ads'); |
84
|
|
|
|
85
|
|
|
$result = $collection->find()->toArray(); |
86
|
|
|
|
87
|
|
|
$tab = []; |
88
|
|
|
foreach($result as $one){ |
89
|
|
|
$tab[] = $one->bsonSerialize(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return response()->json($tab); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.