Completed
Push — master ( ccc97e...383e32 )
by Julien
02:28
created

MoviesController::activate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 24
loc 24
rs 8.9714
cc 2
eloc 12
nc 2
nop 1
1
<?php
2
// chemin relatif ou se trouve la classe
3
namespace App\Http\Controllers;
4
5
6
use App\Http\Models\Movies;
7
use App\Http\Requests\MoviesRequest;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\Redirect;
10
use Illuminate\Support\Facades\Session;
11
12
13
/**
14
 * Class MainController
15
 * @package App\Http\Controllers
16
 * Sufficé par le mot clef Controller
17
 * et doit hérité de la super classe Controller
18
 */
19
class MoviesController extends Controller{
20
21
    /**
22
     * Page Acceuil
23
     */
24
    public function index(){
25
26
        // je crée un obet de mon model Movies
27
        $movies = Movies::all();
28
29
        // le transporteur:
30
        // je transporte mes données du Controlleur
31
        // à la vue
32
        return view('Movies/index',[
33
                'movies' => $movies
34
            ]
35
        );
36
    }
37
38
    /**
39
     * Page Create
40
     */
41
    public function create(){
42
43
        // vue
44
        return view('Movies/create');
45
    }
46
47
48
    /**
49
     * Page Read
50
     */
51
    public function read($id){
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
53
        // vue
54
        return view('Movies/read');
55
    }
56
    /**
57
     * Page Read
58
     */
59
    public function edit($id){
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
61
        // vue
62
        return view('Movies/edit');
63
    }
64
65
66
    /**
67
     * Action qui va me permettre d'activer un film
68
     * @param $id
69
     */
70 View Code Duplication
    public function activate($id){
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...
71
        // find() permet de retourner un Objet Movie
72
        // depuis son id
73
        $movie = Movies::find($id);
74
75
        if($movie->visible == 0){
76
            $movie->visible = 1;
77
            Session::flash("success",
78
                "Le film {$movie->title} a bien été activé");
79
80
        }
81
        else{
82
            $movie->visible = 0;
83
            Session::flash("success",
84
                "Le film {$movie->title} a bien été desactivé");
85
        }
86
87
        // save() permet de sauvegarder
88
        // mon objet modifié en BDD
89
        $movie->save();
90
91
        return Redirect::route('movies_index');
92
93
    }
94
95
96
    /**
97
     * Action qui va me permettre de metter en avant un film
98
     * @param $id
99
     */
100 View Code Duplication
    public function cover($id){
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...
101
        // find() permet de retourner un Objet Movie
102
        // depuis son id
103
        $movie = Movies::find($id);
104
105
        if($movie->cover == 0){
106
            $movie->cover = 1;
107
            Session::flash("success",
108
                "Le film {$movie->title} a bien été mis en avant");
109
110
        }
111
        else{
112
            $movie->cover = 0;
113
            Session::flash("danger",
114
                "Le film {$movie->title} a bien été retiré de l'avant");
115
        }
116
117
        // save() permet de sauvegarder
118
        // mon objet modifié en BDD
119
        $movie->save();
120
121
        return Redirect::route('movies_index');
122
123
    }
124
125
126
127
    /**
128
     * Suppresion
129
     */
130
    public function delete($id){
131
132
        // Un objet film depuis son ID
133
        $movie = Movies::find($id);
134
135
        // si le film n'existe plus
136
        if($movie){
137
138
            // Créer un message flash de type "success"
139
            Session::flash('success', "Le film {$movie->title} a bien été supprimé");
140
141
            // delete() permete de supprimer un objet en
142
            // base de données
143
144
            $movie->delete();
145
        }
146
147
148
149
150
        //redirection vers mes films
151
        return Redirect::route('movies_index');
152
    }
153
154
155
156
    /**
157
     * Action d'enregistrement en base de données
158
     * depuis mon formulaire
159
     * Classe Request permet de réceptionner les données
160
     * en POST de manières scurisés
161
     *
162
     * MoviesRequest représente mon formulaire
163
     * et la requete en POST de mon formulaire
164
     *
165
     * Je rentre dans ma methode store
166
     * SI ET SEULEMENT SI
167
     * je n'ai plus aucune erreur dans mon formulaire
168
     */
169
    public function store(MoviesRequest $request){
170
171
        //crée un objet qui représente mon nouveau film
172
        $movie = new Movies();
173
        $movie->type = $request->type;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<App\Http\Models\Movies>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
Bug introduced by
The property type does not seem to exist in App\Http\Requests\MoviesRequest.

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...
174
        $movie->title = $request->title;
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<App\Http\Models\Movies>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
Bug introduced by
The property title does not seem to exist in App\Http\Requests\MoviesRequest.

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...
175
        $movie->synopsis = $request->synopsis;
0 ignored issues
show
Documentation introduced by
The property synopsis does not exist on object<App\Http\Models\Movies>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
Bug introduced by
The property synopsis does not seem to exist in App\Http\Requests\MoviesRequest.

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...
176
        $movie->description = $request->description;
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<App\Http\Models\Movies>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
Bug introduced by
The property description does not seem to exist in App\Http\Requests\MoviesRequest.

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...
177
178
179
        /* Traitement de l'upload d'image */
180
        $filename = "";
181
182
        // Si j'ai un fichier "image"
183
        if($request->hasFile('image')) {
184
185
            // je récupere mon fichier
186
            $file = $request->file('image');
187
188
            // je récupere le nom du fichier
189
            $filename = $file->getClientOriginalName(); // Récupère le nom original du fichier
190
191
            // je stocker le chemin vers lequele mon image va etre envoyé
192
            $destinationPath = public_path() . '/uploads/movies'; // Indique où stocker le fichier
193
194
            // je bouge mon fichier uploadé
195
            $file->move($destinationPath, $filename); // Déplace le fichier
196
        }
197
198
        //je renseigne le nom de mon image pour mon film
199
        $movie->image = asset("uploads/movies/". $filename);
0 ignored issues
show
Documentation introduced by
The property image does not exist on object<App\Http\Models\Movies>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
200
201
202
        //sauevegarder mon objet Movies en BDD
203
        $movie->save();
204
205
206
207
208
        // je cére un message flash
209
        Session::flash('success', "Le film {$movie->title} a été enregistré");
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<App\Http\Models\Movies>. 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...
210
211
        // redirection vers movies_index
212
        return Redirect::route('movies_index');
213
214
215
//        $movie->trailer = $request->trailer;
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
216
//        $movie->categories_id = $request->categories_id;
217
//        $movie->languages = $request->lang;
218
//        $movie->distributeur = $request->distributeur;
219
//        $movie->bo = $request->bo;
220
//        $movie->annee = $request->annee;
221
//        $movie->budget = $request->budget;
222
//        $movie->duree = $request->duree;
223
//        $movie->date_release = $request->date_release;
224
//        $movie->note_presse = $request->note_presse;
225
//        $movie->visible = $request->visible;
226
//        $movie->cover = $request->cover;
227
//
228
//
229
//        /* Traitement de l'upload d'image */
230
//        $filename = "";
231
//        if($request->hasFile('image')) {
232
//            $file = $request->file('image');
233
//            $filename = $file->getClientOriginalName(); // Récupère le nom original du fichier
234
//            $destinationPath = public_path() . '/uploads/movies'; // Indique où stocker le fichier
235
//            $file->move($destinationPath, $filename); // Déplace le fichier
236
//        }
237
//        $movie->image = asset("uploads/movies/". $filename);
238
//
239
//        $movie->save();
240
//
241
//
242
//        // Traitement des champs de la relations Actors_Movies
243
//        $actors = $request->actors;
244
//        if (isset($actors)) {
245
//            foreach ($actors as $actor) {
246
//                DB::table('actors_movies')
247
//                    ->insert([
248
//                        ['movies_id' => $movie->id, 'actors_id' => $actor]
249
//                    ]);
250
//            }
251
//        }
252
//
253
//        // Traitement des champs de la relations Directors_Movies
254
//        $directors = $request->directors;
255
//        if (isset($directors)) {
256
//            foreach ($directors as $director) {
257
//                DB::table('directors_movies')
258
//                    ->insert([
259
//                        ['movies_id' => $movie->id, 'directors_id' => $director]
260
//                    ]);
261
//            }
262
//        }
263
//
264
//
265
//        Session::flash('success', "Le film $movie->title a été enregistré");
266
//
267
//        return Redirect::route('movies_index');
268
    }
269
270
271
}
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290