Completed
Push — master ( dd0ca4...f00659 )
by Julien
03:18
created

AdministratorsController::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
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 13
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
// chemin relatif ou se trouve la classe
3
namespace App\Http\Controllers;
4
5
use App\Http\Models\Administrators;
6
use App\Http\Requests\AdministratorsRequest;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Redirect;
10
use Illuminate\Support\Facades\Session;
11
12
/**
13
 * Class AdministratorsController
14
 */
15
class AdministratorsController extends Controller{
16
17
    /**
18
     * Page de liste des administrateurs
19
     */
20
    public function index(){
21
        $administrators = Administrators::all();
22
23
        return view('Administrators/index',[
24
            'administrators' => $administrators
25
        ]);
26
    }
27
28
29
    /**
30
     * To remove an administrators
31
     * @param $id
32
     * @return mixed
33
     */
34
    public function remove($id){
35
36
        // To load an administrator
37
        $administrator = Administrators::find($id);
38
39
        if($administrator){
40
            Session::flash('success', "L'administrateur {$administrator->email} a bien été supprimé");
41
            $administrator->delete();
42
        }
43
44
        return Redirect::route('administrators_index');
45
46
    }
47
48
49
    /**
50
     * To remove an administrators
51
     * @param $id
52
     * @return mixed
53
     */
54
    public function create(){
55
56
        return view('Administrators/create');
57
    }
58
    /**
59
     * To remove an administrators
60
     * @param $id
61
     * @return mixed
62
     */
63
    public function store(AdministratorsRequest $request, $id = null){
64
65
        //creation
66
        if($id == null){
67
            $administrator = new Administrators();
68
        }else{
69
            $administrator = Administrators::find($id);
70
        }
71
72
        $administrator->firstname = $request->firstname;
0 ignored issues
show
Bug introduced by
The property firstname does not seem to exist in App\Http\Requests\AdministratorsRequest.

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...
73
        $administrator->lastname = $request->lastname;
0 ignored issues
show
Bug introduced by
The property lastname does not seem to exist in App\Http\Requests\AdministratorsRequest.

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...
74
        $administrator->email = $request->email;
0 ignored issues
show
Bug introduced by
The property email does not seem to exist in App\Http\Requests\AdministratorsRequest.

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...
75
        $administrator->description = $request->description;
0 ignored issues
show
Bug introduced by
The property description does not seem to exist in App\Http\Requests\AdministratorsRequest.

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...
76
        $administrator->password = bcrypt($request->password); //if en mode edit
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<App\Http\Requests\AdministratorsRequest>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
77
        $administrator->super_admin =  $request->super_admin;
0 ignored issues
show
Bug introduced by
The property super_admin does not seem to exist in App\Http\Requests\AdministratorsRequest.

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...
78
        $administrator->expiration_date = new \DateTime('+1 year');
79
        $administrator->active = true;
80
81
        //upload
82
        $filename = "";
0 ignored issues
show
Unused Code introduced by
$filename 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...
83
        if($request->hasFile('image')) {
84
            $file = $request->file('image');
85
            $filename = $file->getClientOriginalName(); // Récupère le nom original du fichier
86
            $destinationPath = public_path() . '/uploads/administrators'; // Indique où stocker le fichier
87
            $file->move($destinationPath, $filename); // Déplace le fichier
88
            $administrator->photo = asset("uploads/administrators/". $filename);
89
        }
90
91
        $administrator->save();
92
93
       // Auth::login($administrator); => Autologin
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
        Session::flash('success', "L'administrateur {$administrator->email} a bien été crée");
95
        return Redirect::route('administrators_index');
96
97
    }
98
99
100
101
102
    /**
103
     * To edit an administrator
104
     * @param $id
105
     * @return mixed
106
     */
107
    public function edit($id){
108
109
        $administrator = Administrators::find($id);
110
111
        return view('Administrators/edit', [
112
            'administrator' => $administrator
113
        ]);
114
    }
115
116
117
118
119
}
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138