Passed
Push — master ( 819cbf...086d7c )
by Nicklas
02:29
created

UserController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Nicklas\Comment;
4
5
use \Anax\Configure\ConfigureInterface;
6
use \Anax\Configure\ConfigureTrait;
7
use \Anax\DI\InjectionAwareInterface;
8
use \Anax\DI\InjectionAwareTrait;
9
use \Nicklas\Comment\HTMLForm\UserLoginForm;
10
use \Nicklas\Comment\HTMLForm\CreateUserForm;
11
use \Nicklas\Comment\HTMLForm\EditProfileForm;
12
13
/**
14
 * A controller class.
15
 */
16
class UserController implements
17
    ConfigureInterface,
18
    InjectionAwareInterface
19
{
20
    use ConfigureTrait, InjectionAwareTrait;
21
22
23
24
    /**
25
     * @var $data description
26
     */
27
    //private $data;
28
29
30
31
    /**
32
     * Get details on item to load form with.
33
     *
34
     * @param integer $id get details on item with id.
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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
     * @return object true if okey, false if something went wrong.
37
     */
38 2
    public function getUserDetails($name)
39
    {
40 2
        $user = new User();
41 2
        $user->setDb($this->di->get("db"));
42 2
        $user->find("name", $name);
43 2
        $user->setGravatar();
44 2
        return $user;
45
    }
46
47
    /**
48
     * Logout user by setting "user" == null in session.
49
     *
50
     *
51
     * @return void
52
     */
53
    public function logout()
54
    {
55
        $this->di->get('session')->set("user", null);
56
        $this->di->get("response")->redirect("user/login");
57
    }
58
59
60
    /**
61
     * Render page for users
62
     *
63
     * @return void
64
     */
65 2
    public function renderPage($views, $title)
66
    {
67 2
        $this->di->get("pageRenderComment")->renderPage([
68 2
            "views" => $views,
69
            "title" => "$title"
70 2
        ]);
71 2
    }
72
73
74
    /**
75
     * check if user is logged in
76
     *
77
     * @return void
78
     */
79 2
    public function checkIsLogin()
80
    {
81 2
        if (!$this->di->get("session")->has("user")) {
82
            $views = [
83 1
                ["user/fail", [], "main"]
84 1
            ];
85 1
            $this->renderPage($views, "Not logged in");
86 1
        }
87 2
    }
88
89
90
91
92
    /**
93
     * Description.
94
     *
95
     * @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...
96
     *
97
     * @throws Exception
98
     *
99
     * @return void
100
     */
101 1 View Code Duplication
    public function getPostLogin()
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...
102
    {
103 1
        $form       = new UserLoginForm($this->di);
104 1
        $form->check();
105
106
        $views = [
107 1
            ["user/login", ["form" => $form->getHTML()], "main"]
108 1
        ];
109
110 1
        $this->renderPage($views, "A login page");
111 1
    }
112
113
114
115
    /**
116
     * Description.
117
     *
118
     * @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...
119
     *
120
     * @throws Exception
121
     *
122
     * @return void
123
     */
124 1 View Code Duplication
    public function getPostCreateUser()
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...
125
    {
126 1
        $form       = new CreateUserForm($this->di);
127 1
        $form->check();
128
129
        $views = [
130 1
            ["user/create", ["form" => $form->getHTML()], "main"]
131 1
        ];
132
133 1
        $this->renderPage($views, "Create a user");
134 1
    }
135
136
    /**
137
     * Description.
138
     *
139
     * @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...
140
     *
141
     * @throws Exception
142
     *
143
     * @return void
144
     */
145 1
    public function getPostEditUser()
146
    {
147 1
        $name = $this->di->get('session')->get("user");
148 1
        $user = $this->getUserDetails($name);
149
150 1
        $form       = new EditProfileForm($this->di, $name);
151 1
        $form->check();
152
153
        $views = [
154 1
            ["user/profile/edit", ["form" => $form->getHTML(), "user" => $user], "main"]
155 1
        ];
156
157 1
        $this->renderPage($views, "A create user page");
158 1
    }
159
160
161
    /**
162
     * Render profile page
163
     *
164
     * @return void
165
     */
166 1
    public function renderProfile()
167
    {
168 1
        $this->checkIsLogin();
169
170 1
        $name = $this->di->get('session')->get("user");
171 1
        $user = $this->getUserDetails($name);
172
173
174
        $views = [
175 1
            ["user/profile/profile", ["user" => $user], "main"]
176 1
        ];
177
178 1
        if ($user->authority == "admin") {
179
            $views = [
180
                ["admin/navbar", [], "main"],
181
                ["user/profile/profile", ["user" => $user], "main"]
182
            ];
183
        }
184
185 1
        $this->renderPage($views, "$user->name");
186 1
    }
187
}
188