Passed
Push — master ( 44e581...a3411b )
by Nicklas
02:21
created

AdminController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 38.61 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 76.74%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 39
loc 101
ccs 33
cts 43
cp 0.7674
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsers() 0 5 1
A checkIsAdmin() 9 17 2
A getUsersIndex() 0 12 1
A getPostAdminEditUser() 15 15 1
A getPostAdminCreateUser() 15 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nicklas\Comment;
4
5
use \Nicklas\Comment\HTMLForm\Admin\EditUserForm;
6
use \Nicklas\Comment\HTMLForm\Admin\CreateUserForm2;
7
8
// MODULES
9
use \Nicklas\Comment\Modules\User;
10
11
/**
12
 * A controller class.
13
 */
14
class AdminController extends UserController
15
{
16
17
    /**
18
     * Get all users
19
     *
20
     * @return array
21
     */
22 2
    public function getUsers()
23
    {
24 2
        $user = new User($this->di->get("db"));
25 2
        return $user->getAllUsers();
26
    }
27
28
    /**
29
     * check if user is logged in
30
     *
31
     * @return void
32
     */
33 1
    public function checkIsAdmin()
34
    {
35 1
        $this->checkIsLogin();
36
37 1
        $user = new User($this->di->get("db"));
38 1
        $user = $user->getUser($this->di->get("session")->get("user"));
39
40 1 View Code Duplication
        if ($user->authority != "admin") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41
            $views = [
42 1
                ["comment/admin/fail", [], "main"]
43 1
            ];
44 1
            $this->di->get("pageRenderComment")->renderPage([
45 1
                "views" => $views,
46
                "title" => "Not authorized"
47 1
            ]);
48 1
        }
49 1
    }
50
51
52
    /**
53
     * Description.
54
     *
55
     * @return void
56
     */
57 1
    public function getUsersIndex()
58
    {
59
        $views = [
60 1
            ["comment/admin/navbar", [], "main"],
61 1
            ["comment/admin/crud/view-all", ["users" => $this->getUsers()], "main"]
62 1
        ];
63
64 1
        $this->di->get("pageRenderComment")->renderPage([
65 1
            "views" => $views,
66
            "title" => "A collection of users"
67 1
        ]);
68 1
    }
69
70
71
    /**
72
     * Description.
73
     *
74
     * @param int $id
75
     *
76
     * @return void
77
     */
78 1 View Code Duplication
    public function getPostAdminEditUser($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...
79
    {
80 1
        $form = new EditUserForm($this->di, $id);
81 1
        $form->check();
82
83
        $views = [
84 1
            ["comment/admin/navbar", [], "main"],
85 1
            ["comment/admin/crud/edit", ["form" => $form->getHTML()], "main"]
86 1
        ];
87
88 1
        $this->di->get("pageRenderComment")->renderPage([
89 1
            "views" => $views,
90
            "title" => "Edit user"
91 1
        ]);
92 1
    }
93
94
    /**
95
     * Description.
96
     *
97
     * @return void
98
     */
99 View Code Duplication
    public function getPostAdminCreateUser()
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...
100
    {
101
        $form = new CreateUserForm2($this->di);
102
        $form->check();
103
104
        $views = [
105
            ["comment/admin/navbar", [], "main"],
106
            ["comment/admin/crud/edit", ["form" => $form->getHTML()], "main"]
107
        ];
108
109
        $this->di->get("pageRenderComment")->renderPage([
110
            "views" => $views,
111
            "title" => "Create user"
112
        ]);
113
    }
114
}
115