Passed
Push — master ( 2da97c...99f011 )
by Magnus
02:14
created

UserController::getAllUsersPublic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Radchasay\User;
4
5
use \Anax\Configure\ConfigureInterface;
6
use \Anax\Configure\ConfigureTrait;
7
use \Anax\DI\InjectionAwareInterface;
8
use \Anax\Di\InjectionAwareTrait;
9
use Radchasay\User\HTMLForm\AdminUpdateUser;
10
use Radchasay\User\HTMLForm\AdminCreateUserForm;
11
use Radchasay\User\HTMLForm\UpdateProfileForm;
12
use \Radchasay\User\HTMLForm\UserLoginForm;
13
use \Radchasay\User\HTMLForm\CreateUserForm;
14
use \Radchasay\User\HTMLForm\AdminDeleteUserForm;
15
16
/**
17
 * A controller class.
18
 */
19
class UserController implements
20
    ConfigureInterface,
21
    InjectionAwareInterface
22
{
23
    use ConfigureTrait,
24
        InjectionAwareTrait;
25
26
27
    /**
28
     * @var $data description
29
     */
30
    //private $data;
31
32
33
    /**
34
     * Description.
35
     *
36
     * @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...
37
     *
38
     * @throws Exception
39
     *
40
     * @return void
41
     */
42
    public function getIndex()
43
    {
44
        $title = "A index page";
45
        $view = $this->di->get("view");
46
        $pageRender = $this->di->get("pageRender");
47
48
        $data = [
49
            "content" => "An index page",
50
        ];
51
52
        $view->add("default1/article", $data);
53
54
        $pageRender->renderPage(["title" => $title]);
55
    }
56
57
58
    /**
59
     * Description.
60
     *
61
     * @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...
62
     *
63
     * @throws Exception
64
     *
65
     * @return void
66
     */
67
    public function getPostLogin()
68
    {
69
        $title = "A login page";
70
        $view = $this->di->get("view");
71
        $pageRender = $this->di->get("pageRender");
72
        $url = $this->di->get("url");
73
        $response = $this->di->get("response");
74
        $session = $this->di->get("session");
75
        if ($session->has("email")) {
76
            $url = $url->create("user/profile");
77
            $response->redirect($url);
78
        } else {
79
            $form = new UserLoginForm($this->di);
80
81
            $form->check();
82
83
            $data = [
84
                "content" => $form->getHTML(),
85
            ];
86
87
            $view->add("default1/article", $data);
88
89
            $pageRender->renderLoginAndCreate(["title" => $title]);
90
        }
91
    }
92
93
94
    /**
95
     * Description.
96
     *
97
     * @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...
98
     *
99
     * @throws Exception
100
     *
101
     * @return void
102
     */
103
    public function getPostCreateUser()
104
    {
105
        $this->di->get("session")->set("create", "true");
106
        $title = "A create user page";
107
        $view = $this->di->get("view");
108
        $pageRender = $this->di->get("pageRender");
109
        $form = new CreateUserForm($this->di);
110
111
        $form->check();
112
113
        $data = [
114
            "content" => $form->getHTML(),
115
        ];
116
117
        $view->add("default1/article", $data);
118
119
        $pageRender->renderLoginAndCreate(["title" => $title]);
120
    }
121
122
123 View Code Duplication
    public function getUserProfile()
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...
124
    {
125
        $title = "Profile";
126
        $view = $this->di->get("view");
127
        $pageRender = $this->di->get("pageRender");
128
        $user = new User();
129
        $user->setDb($this->di->get("db"));
130
        $session = $this->di->get("session");
131
        $data = [
132
            "content" => $user->getInformation($session->get("email")),
133
        ];
134
135
        $view->add("users/profile", $data);
136
137
        $pageRender->renderPage(["title" => $title]);
138
    }
139
140
    public function logout()
141
    {
142
        $url = $this->di->get("url");
143
        $response = $this->di->get("response");
144
        $session = $this->di->get("session");
145
        $login = $url->create("user/login");
146
147
        if ($session->has("email")) {
148
            $session->delete("email");
149
            $response->redirect($login);
150
        } else {
151
            $response->redirect($login);
152
        }
153
154
        $hasSession = session_status() == PHP_SESSION_ACTIVE;
155
156
        if (!$hasSession) {
157
            $response->redirect($login);
158
            return true;
159
        }
160
    }
161
162
    public function checkLogin()
163
    {
164
        $url = $this->di->get("url");
165
        $response = $this->di->get("response");
166
167
        $login = $url->create("user/login");
168
        $hasSession = session_status() == PHP_SESSION_ACTIVE;
169
        if (!$hasSession) {
170
            $response->redirect($login);
171
            return true;
172
        }
173
    }
174
175 View Code Duplication
    public function editProfile($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...
176
    {
177
        if ($this->checkUserIdMatch($id)) {
178
            $title = "Update an item";
179
            $view = $this->di->get("view");
180
            $pageRender = $this->di->get("pageRender");
181
            $form = new UpdateProfileForm($this->di, $id);
182
183
            $form->check();
184
185
            $data = [
186
                "form" => $form->getHTML(),
187
            ];
188
189
            $view->add("users/editProfile", $data);
190
191
            $pageRender->renderPage(["title" => $title]);
192
        }
193
    }
194
195
196 View Code Duplication
    public function getAllUsers()
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...
197
    {
198
        if ($this->checkAdminLoggedIn()) {
199
            $title = "A collection of items";
200
            $view = $this->di->get("view");
201
            $pageRender = $this->di->get("pageRender");
202
            $db = $this->di->get("db");
203
            $user = new User();
204
            $user->setDb($db);
205
206
            $data = [
207
                "items" => $user->findAll(),
208
            ];
209
210
            $view->add("admin/viewUsers", $data);
211
212
            $pageRender->renderPage(["title" => $title]);
213
        }
214
    }
215
216 View Code Duplication
    public function getAllUsersPublic()
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...
217
    {
218
        $title = "All Users";
219
        $view = $this->di->get("view");
220
        $pageRender = $this->di->get("pageRender");
221
        $db = $this->di->get("db");
222
        $user = new User();
223
        $user->setDb($db);
224
225
        $data = [
226
            "items" => $user->findAll(),
227
        ];
228
229
        $view->add("users/showAll", $data);
230
231
        $pageRender->renderPage(["title" => $title]);
232
    }
233
234 View Code Duplication
    public function createUser()
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...
235
    {
236
        if ($this->checkAdminLoggedIn()) {
237
            $this->checkAdminLoggedIn();
238
            $title = "Create a item";
239
            $view = $this->di->get("view");
240
            $pageRender = $this->di->get("pageRender");
241
            $form = new AdminCreateUserForm($this->di);
242
243
            $form->check();
244
245
            $data = [
246
                "form" => $form->getHTML(),
247
            ];
248
249
            $view->add("admin/create", $data);
250
251
            $pageRender->renderPage(["title" => $title]);
252
        }
253
    }
254
255
256 View Code Duplication
    public function deleteUser()
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...
257
    {
258
        if ($this->checkAdminLoggedIn()) {
259
            $title = "Delete an item";
260
            $view = $this->di->get("view");
261
            $pageRender = $this->di->get("pageRender");
262
            $form = new AdminDeleteUserForm($this->di);
263
264
            $form->check();
265
266
            $data = [
267
                "form" => $form->getHTML(),
268
            ];
269
270
            $view->add("admin/delete", $data);
271
272
            $pageRender->renderPage(["title" => $title]);
273
        }
274
    }
275
276 View Code Duplication
    public function updateUser($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...
277
    {
278
        if ($this->checkAdminLoggedIn()) {
279
            $title = "Update an item";
280
            $view = $this->di->get("view");
281
            $pageRender = $this->di->get("pageRender");
282
            $form = new AdminUpdateUser($this->di, $id);
283
284
            $form->check();
285
286
            $data = [
287
                "form" => $form->getHTML(),
288
            ];
289
290
            $view->add("admin/update", $data);
291
292
            $pageRender->renderPage(["title" => $title]);
293
        }
294
    }
295
296
    public function checkUserIdMatch($id)
297
    {
298
        $url = $this->di->get("url");
299
        $response = $this->di->get("response");
300
        $session = $this->di->get("session");
301
        $db = $this->di->get("db");
302
303
        if ($session->has("email")) {
304
            $email = $session->get("email");
305
            $user = new User();
306
            $user->setDb($db);
307
            $res = $user->find("email", $email);
308
            if ($res->id != $id) {
309
                $url = $url->create("user/profile");
310
                $response->redirect($url);
311
                return false;
312
            }
313
            return true;
314
        } else {
315
            $url = $url->create("user/profile");
316
            $response->redirect($url);
317
        }
318
    }
319
320
    public function checkAdminLoggedIn()
321
    {
322
        $url = $this->di->get("url");
323
        $response = $this->di->get("response");
324
        $session = $this->di->get("session");
325
        $db = $this->di->get("db");
326
327
        if ($session->has("email")) {
328
            $email = $session->get("email");
329
            $user = new User();
330
            $user->setDb($db);
331
            $res = $user->find("email", $email);
332
333
            if (!$res->permissions == "admin" || $res->permissions == "user") {
334
                $url = $url->create("user/login");
335
                $response->redirect($url);
336
            }
337
            return true;
338
        } else {
339
            $url = $url->create("user/login");
340
            $response->redirect($url);
341
        }
342
    }
343
344
    public function checkLoginPage()
345
    {
346
        $session = $this->di->get("session");
347
348
        if (!$session->has("email")) {
349
            if ($session->has("create")) {
350
                return $this->getPostCreateUser();
351
            } else {
352
                return $this->getPostLogin();
353
            }
354
        }
355
    }
356
357
358
    public function checkIfLoggedIn()
359
    {
360
        return $this->checkLoginPage();
361
    }
362
}
363