UserController::createUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 20
Ratio 100 %

Importance

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