Completed
Push — master ( 33cf5d...db4373 )
by Julien
04:23
created

AdController::adAnnounce()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 59
rs 8.9846
cc 4
eloc 38
nc 4
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
0 ignored issues
show
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
31
        $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...
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()
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...
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