Passed
Push — master ( 592d4f...1560d7 )
by Martin
01:34
created

UserController::postCreatingUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 51
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 51
ccs 0
cts 26
cp 0
rs 9.4109
cc 3
eloc 25
nc 3
nop 0
crap 12

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 Mafd16\User;
4
5
use \Anax\Configure\ConfigureInterface;
6
use \Anax\Configure\ConfigureTrait;
7
use \Anax\DI\InjectionAwareInterface;
8
use \Anax\Di\InjectionAwareTrait;
9
use \Anax\User\HTMLForm\UserLoginForm;
10
use \Anax\User\HTMLForm\CreateUserForm;
11
12
/**
13
 * A controller class.
14
 */
15
class UserController implements
16
    ConfigureInterface,
17
    InjectionAwareInterface
18
{
19
    use ConfigureTrait,
20
        InjectionAwareTrait;
21
22
23
24
    /**
25
     * @var $data description
26
     */
27
    //private $data;
28
29
30
31
    /**
32
     * Description.
33
     *
34
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
     *
36
     * @throws Exception
37
     *
38
     * @return void
39
     */
40
    public function getIndex()
41
    {
42
        $title      = "A index page";
43
        $view       = $this->di->get("view");
44
        $pageRender = $this->di->get("pageRender");
45
46
        $data = [
47
            "content" => "An index page",
48
        ];
49
50
        $view->add("default2/article", $data);
51
52
        $pageRender->renderPage(["title" => $title]);
53
    }
54
55
56
57
    /**
58
     * Description.
59
     *
60
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
61
     *
62
     * @throws Exception
63
     *
64
     * @return void
65
     */
66 View Code Duplication
    public function getLogin()
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...
67
    {
68
        $title      = "A login page";
69
        $view       = $this->di->get("view");
70
        $pageRender = $this->di->get("pageRender");
71
72
        $data = [
73
            "message" => "",
74
        ];
75
76
        $view->add("user/crud/login", $data);
77
78
79
        $pageRender->renderPage(["title" => $title]);
80
    }
81
82
83
84
    /**
85
     * Description.
86
     *
87
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
88
     *
89
     * @throws Exception
90
     *
91
     * @return void
92
     */
93 View Code Duplication
    public function getCreateUser($message = null)
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...
94
    {
95
        $title      = "A create user page";
96
        $view       = $this->di->get("view");
97
        $pageRender = $this->di->get("pageRender");
98
99
100
        $data = [
101
            "message" => $message,
102
        ];
103
104
        $view->add("user/crud/create", $data);
105
106
        $pageRender->renderPage(["title" => $title]);
107
    }
108
109
110
111
    /**
112
     * Description.
113
     *
114
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
115
     *
116
     * @throws Exception
117
     *
118
     * @return void
119
     */
120
    public function postCreatingUser()
121
    {
122
        $request    = $this->di->get("request");
123
        $session    = $this->di->get("session");
124
125
        // Get POST-variables
126
        $acronym = $request->getPost("name");
127
        $email = $request->getPost("email");
128
        $password = $request->getPost("password");
129
        $passwordagain = $request->getPost("passwordagain");
130
131
        if ($password !== $passwordagain) {
132
            $message = "<p>Passwords did not match!</p>";
133
            $this->getCreateUser($message);
134
            return;
135
        }
136
        /*
137
        // Get user from db
138
        $user = new User();
139
        $user->setDb($this->di->get("db"));
140
        // Update $user:
141
        $user->email = $email;
142
        $user->setPassword($password);
143
        $user->acronym = $acronym;
144
        $user->admin = 0;
145
        // Save to database
146
        $user->save();
147
        */
148
        $newUser = [
149
            "acronym" => $acronym,
150
            "password" => $password,
151
            "email" => $email,
152
        ];
153
154
        $isCreated = $this->di->get("user")->createUser($newUser);
155
156
        if (!$isCreated) {
157
            $message = "<p>User " . $acronym . " already exists!</p>";
158
            $this->getCreateUser($message);
159
            return;
160
        }
161
162
163
        // Save user to session
164
        $session->set("my_user_id", $user->id);
0 ignored issues
show
Bug introduced by
The variable $user does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
165
        $session->set("my_user_name", $user->acronym);
166
        $session->set("my_user_email", $user->email);
167
        $session->set("my_user_admin", $user->admin);
168
        // Redirect back to profile
169
        $this->di->get("response")->redirect("user/profile");
170
    }
171
172
173
174
    /**
175
     * Description.
176
     *
177
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
178
     *
179
     * @throws Exception
180
     *
181
     * @return void
182
     */
183
    public function getUserProfile()
184
    {
185
        $title      = "A profile page";
186
        $view       = $this->di->get("view");
187
        $pageRender = $this->di->get("pageRender");
188
        $session    = $this->di->get("session");
189
        // Get user from db
190
        $user = new User();
191
        $user->setDb($this->di->get("db"));
192
        $id = $session->get("my_user_id");
193
        $user->find("id", $id);
194
195
        $data = [
196
            "user" => $user,
197
        ];
198
199
        $view->add("user/crud/profile", $data);
200
        $pageRender->renderPage(["title" => $title]);
201
    }
202
203
204
205
    /**
206
     * Description.
207
     *
208
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
209
     *
210
     * @throws Exception
211
     *
212
     * @return void
213
     */
214
    public function validateUser()
215
    {
216
        $view       = $this->di->get("view");
217
        $pageRender = $this->di->get("pageRender");
218
        $request    = $this->di->get("request");
219
        $session    = $this->di->get("session");
220
221
        // Connect to database
222
        $user = new User();
223
        $user->setDb($this->di->get("db"));
224
        // Get POST-variables
225
        $acronym = $request->getPost("name");
226
        $password = $request->getPost("password");
227
        // Get the user from DB
228
        $user->find("acronym", $acronym);
229
230
        if ($user->deleted) {
231
            $title = "A login page";
232
            $data = [
233
                "message" => $user->acronym . " were deleted " . $user->deleted,
234
            ];
235
            $view->add("user/crud/login", $data);
236
            $pageRender->renderPage(["title" => $title]);
237
        }
238
239
        // Validate password against database
240
        $valid = $user->verifyPassword($acronym, $password);
241
        // if true, save user id to session and goto profile
242
        if ($valid) {
243
            // Save user id to session
244
            $session->set("my_user_id", $user->id);
245
            $session->set("my_user_name", $user->acronym);
246
            $session->set("my_user_email", $user->email);
247
            $session->set("my_user_admin", $user->admin);
248
            $this->di->get("response")->redirect("user/profile");
249
        } else {
250
            // if false goto login
251
            $title = "A login page";
252
            $data = [
253
                "acronym" => $acronym,
254
                "password" => $password,
255
                "valid" => $valid,
256
                "message" => "Name or password was incorrect!",
257
            ];
258
            $view->add("user/crud/login", $data);
259
            $pageRender->renderPage(["title" => $title]);
260
        }
261
    }
262
263
264
265
    /**
266
     * Description.
267
     *
268
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
269
     *
270
     * @throws Exception
271
     *
272
     * @return void
273
     */
274 View Code Duplication
    public function updateGetUserProfile($message = null)
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...
275
    {
276
        $title      = "Update user";
277
        $view       = $this->di->get("view");
278
        $pageRender = $this->di->get("pageRender");
279
280
        // Get user from db
281
        $user = new User();
282
        $user->setDb($this->di->get("db"));
283
        $id = $this->di->get("session")->get("my_user_id");
284
        $user->find("id", $id);
285
        $data = [
286
            "user" => $user,
287
            "message" => $message,
288
        ];
289
290
        $view->add("user/crud/update", $data);
291
        $pageRender->renderPage(["title" => $title]);
292
    }
293
294
295
296
    /**
297
     * Description.
298
     *
299
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
300
     *
301
     * @throws Exception
302
     *
303
     * @return void
304
     */
305
    public function updatePostUserProfile()
306
    {
307
        $request    = $this->di->get("request");
308
        $session    = $this->di->get("session");
309
310
        // Get POST-variables
311
        $email = $request->getPost("email");
312
        $password = $request->getPost("password");
313
        $passwordagain = $request->getPost("passwordagain");
314
315
        if ($password !== $passwordagain) {
316
            $message = "<p>Passwords did not match!</p>";
317
            $this->updateGetUserProfile($message);
318
        }
319
320
        // Get user id from session
321
        $id = $session->get("my_user_id");
322
        // Get user from db
323
        $user = new User();
324
        $user->setDb($this->di->get("db"));
325
        $user->find("id", $id);
326
        // Update $user:
327
        $user->email = $email;
328
        if (!empty($password)) {
329
            $user->setPassword($password);
330
        }
331
        // Save to database
332
        $user->save();
333
        // Save to session
334
        $session->set("my_user_id", $user->id);
335
        $session->set("my_user_name", $user->acronym);
336
        $session->set("my_user_email", $user->email);
337
        $session->set("my_user_admin", $user->admin);
338
        // Redirect back to profile
339
        $this->di->get("response")->redirect("user/profile");
340
    }
341
342
343
344
    /**
345
     * Description.
346
     *
347
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
348
     *
349
     * @throws Exception
350
     *
351
     * @return void
352
     */
353
    public function getLogout()
354
    {
355
        // Unset session-key user
356
        $this->di->get("session")->delete("my_user_id");
357
        $this->di->get("session")->delete("my_user_name");
358
        $this->di->get("session")->delete("my_user_email");
359
        $this->di->get("session")->delete("my_user_admin");
360
        // Redirect back to login
361
        $this->di->get("response")->redirect("user/login");
362
    }
363
364
365
366
    /**
367
     * Description.
368
     *
369
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
370
     *
371
     * @throws Exception
372
     *
373
     * @return void
374
     */
375
    public function getAdmin()
376
    {
377
        $title      = "An admin page";
378
        $view       = $this->di->get("view");
379
        $pageRender = $this->di->get("pageRender");
380
381
        // Get users from db
382
        $user = new User();
383
        $user->setDb($this->di->get("db"));
384
385
        $data = [
386
            "users" => $user->findAll(),
387
        ];
388
389
        $view->add("user/admin/index", $data);
390
        $pageRender->renderPage(["title" => $title]);
391
    }
392
393
394
395
396
    /**
397
     * Description.
398
     *
399
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
400
     *
401
     * @throws Exception
402
     *
403
     * @return void
404
     */
405 View Code Duplication
    public function getAdminUpdateUser($message = "", $userId = null)
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...
406
    {
407
        $title      = "Admin update user";
408
        $view       = $this->di->get("view");
409
        $pageRender = $this->di->get("pageRender");
410
411
        // Get user id from GET variable
412
        $userId = isset($userId) ? $userId : $this->di->get("request")->getGet("id");
413
414
        // Get user from db
415
        $user = new User();
416
        $user->setDb($this->di->get("db"));
417
        $user->find("id", $userId);
418
419
        $data = [
420
            "user" => $user,
421
            "message" => $message,
422
        ];
423
424
        $view->add("user/admin/update", $data);
425
        $pageRender->renderPage(["title" => $title]);
426
    }
427
428
429
430
431
    /**
432
     * Description.
433
     *
434
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
435
     *
436
     * @throws Exception
437
     *
438
     * @return void
439
     */
440
    public function postAdminUpdateUser()
441
    {
442
        $request    = $this->di->get("request");
443
444
        // Get POST-variables
445
        $email = $request->getPost("email");
446
        $password = $request->getPost("password");
447
        $passwordagain = $request->getPost("passwordagain");
448
        $admin = $request->getPost("admin");
449
        $userId = $request->getPost("user_id");
450
451
        if ($password !== $passwordagain) {
452
            $message = "<p>Passwords did not match!</p>";
453
            $this->getAdminUpdateUser($message, $userId);
454
        }
455
456
        // Get user id from session
457
        // Get user from db
458
        $user = new User();
459
        $user->setDb($this->di->get("db"));
460
        $user->find("id", $userId);
461
        // Update $user:
462
        $user->email = $email;
463
        $user->admin = $admin;
464
        if (!empty($password)) {
465
            $user->setPassword($password);
466
        }
467
        // Save to database
468
        $user->save();
469
470
        // Redirect back to admin page
471
        $this->di->get("response")->redirect("user/admin");
472
    }
473
474
475
476
    /**
477
     * Description.
478
     *
479
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
480
     *
481
     * @throws Exception
482
     *
483
     * @return void
484
     */
485 View Code Duplication
    public function getAdminCreateUser($message = null)
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...
486
    {
487
        $title      = "Admin create user page";
488
        $view       = $this->di->get("view");
489
        $pageRender = $this->di->get("pageRender");
490
491
        $data = [
492
            "message" => $message,
493
        ];
494
495
        $view->add("user/admin/create", $data);
496
497
        $pageRender->renderPage(["title" => $title]);
498
    }
499
500
501
502
    /**
503
     * Description.
504
     *
505
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
506
     *
507
     * @throws Exception
508
     *
509
     * @return void
510
     */
511
    public function postAdminCreateUser()
512
    {
513
        $request    = $this->di->get("request");
514
515
        // Get POST-variables
516
        $acronym = $request->getPost("name");
517
        $email = $request->getPost("email");
518
        $admin = $request->getPost("admin");
519
        $password = $request->getPost("password");
520
        $passwordagain = $request->getPost("passwordagain");
521
522
        if ($password !== $passwordagain) {
523
            $message = "<p>Passwords did not match!</p>";
524
            $this->getAdminCreateUser($message);
525
            return;
526
        }
527
528
        // Get user from db
529
        $user = new User();
530
        $user->setDb($this->di->get("db"));
531
        // Update $user:
532
        $user->email = $email;
533
        $user->setPassword($password);
534
        $user->acronym = $acronym;
535
        $user->admin = $admin;
536
        // Save to database
537
        $user->save();
538
        // Redirect back to admin
539
        $this->di->get("response")->redirect("user/admin");
540
    }
541
542
543
544
545
    /**
546
     * Description.
547
     *
548
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
549
     *
550
     * @throws Exception
551
     *
552
     * @return void
553
     */
554
    public function getAdminDeleteUser()
555
    {
556
        // Get user to delete from GET variable
557
        $userToDelete = $this->di->get("request")->getGet("id");
558
559
        // Get user from db
560
        $user = new User();
561
        $user->setDb($this->di->get("db"));
562
        $user->find("id", $userToDelete);
563
        // Delete user (Set Deleted to timestamp)
564
        $user->deleted = date("Y-m-d H:i:s");
565
        $user->save();
566
        //$this->di->get("user")->deleteUser($id);
567
568
        // Redirect back to admin
569
        $this->di->get("response")->redirect("user/admin");
570
    }
571
}
572