AdministratorsController::store()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 43
rs 8.5806
cc 4
eloc 24
nc 8
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Models\Administrators;
6
use App\Http\Requests\AdministratorsRequest;
7
use Illuminate\Support\Facades\Redirect;
8
use Illuminate\Support\Facades\Session;
9
10
/**
11
 * Class AdministratorsController.
12
 */
13
class AdministratorsController extends Controller
14
{
15
    /**
16
     * Page de liste des administrateurs.
17
     */
18
    public function index()
19
    {
20
        $administrators = Administrators::all();
21
22
        return view('Administrators/index', [
23
            'administrators' => $administrators,
24
        ]);
25
    }
26
27
    /**
28
     * To remove an administrators.
29
     *
30
     * @param $id
31
     *
32
     * @return mixed
33
     */
34
    public function remove($id)
35
    {
36
37
        // To load an administrator
38
        $administrator = Administrators::find($id);
39
40
        if ($administrator) {
41
            Session::flash('success', "L'administrateur {$administrator->email} a bien été supprimé");
42
            $administrator->delete();
43
        }
44
45
        return Redirect::route('administrators_index');
46
    }
47
48
    /**
49
     * To remove an administrators.
50
     *
51
     * @param $id
52
     *
53
     * @return mixed
54
     */
55
    public function create()
56
    {
57
        return view('Administrators/create');
58
    }
59
60
    /**
61
     * To store an administrators
62
     * ARgument $id est facultatif: null est sa valeur par défaut.
63
     */
64
    public function store(AdministratorsRequest $request, $id = null)
65
    {
66
67
        // creation: créer un nouvel administrateur
68
        if ($id === null) {
69
            $administrator = new Administrators();
70
        } else {
71
            // edition: récupérer un administrateur
72
            $administrator = Administrators::find($id);
73
        }
74
75
        $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...
76
        $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...
77
        $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...
78
        $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...
79
80
        // si l'utilisateur modifie
81
        // son mot de passe en mode edition
82
        if (!empty($request->password)) {
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...
83
            $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...
84
        }
85
86
        $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...
87
        $administrator->expiration_date = new \DateTime('+1 year');
88
        $administrator->active = true;
89
90
        //upload
91
        $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...
92
        if ($request->hasFile('image')) {
93
            $file = $request->file('image');
94
            $filename = $file->getClientOriginalName(); // Récupère le nom original du fichier
95
            $destinationPath = public_path().'/uploads/administrators'; // Indique où stocker le fichier
96
            $file->move($destinationPath, $filename); // Déplace le fichier
97
            $administrator->photo = asset('uploads/administrators/'.$filename);
98
        }
99
100
        $administrator->save();
101
102
       // 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...
103
        Session::flash('success', "L'administrateur {$administrator->email} a bien été crée");
104
105
        return Redirect::route('administrators_index');
106
    }
107
108
    /**
109
     * To edit an administrator.
110
     *
111
     * @param $id
112
     *
113
     * @return mixed
114
     */
115
    public function edit($id)
116
    {
117
        $administrator = Administrators::find($id);
118
119
        return view('Administrators/edit', [
120
            'administrator' => $administrator,
121
        ]);
122
    }
123
}
124