Test Failed
Push — master ( 8e3065...29b5e3 )
by Magnus
02:11
created

getAllPostsAndCommentsFromSpecificUser()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
357
358
        $post = new Post();
359
        $post->setDb($db);
360
        $email = $user->email;
361
        $postInformation = $post->getAllInformationWhere($email);
362
363
        $comment = new Comment();
364
        $comment->setDb($db);
365
        $sql = "Call GetAllCommentsFromSpecific(?)";
366
367
        $data = [
368
            "posts" => $postInformation,
369
            "comments" => $comment->getAllCommentsFromSpecificPost($sql, [$email])
370
        ];
371
372
        $view->add("users/all", $data);
373
374
        $pageRender->renderPage(["title" => $title]);
375
    }
376
377
    public function checkLoginPage()
378
    {
379
        $session = $this->di->get("session");
380
381
        if (!$session->has("email")) {
382
            if ($session->has("create")) {
383
                return $this->getPostCreateUser();
384
            } else {
385
                return $this->getPostLogin();
386
            }
387
        }
388
    }
389
390
391
    public function checkIfLoggedIn()
392
    {
393
        return $this->checkLoginPage();
394
    }
395
}
396