UserController::getPostDeleteUser()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 8.6845
cc 4
eloc 13
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Peto16\User;
4
5
use \Anax\DI\InjectionAwareInterface;
6
use \Anax\DI\InjectionAwareTrait;
7
use \Peto16\User\HTMLForm\UserLoginForm;
8
use \Peto16\User\HTMLForm\CreateUserForm;
9
use \Peto16\User\HTMLForm\UpdateUserForm;
10
use \Peto16\User\HTMLForm\DeleteUserForm;
11
12
/**
13
 * Controller for Login
14
 */
15
class UserController implements InjectionAwareInterface
16
{
17
    use InjectionAwareTrait;
18
19
    private $session;
20
    private $userService;
21
    private $response;
22
    private $view;
23
    private $pageRender;
24
25
26
27
    /**
28
     * Initiate the controller.
29
     * @return void
30
     */
31 8
    public function init()
32
    {
33 8
        $this->userService = $this->di->get("userService");
34 8
        $this->session = $this->di->get("session");
35 8
        $this->response = $this->di->get("response");
36 8
        $this->view = $this->di->get("view");
37 8
        $this->pageRender = $this->di->get("pageRender");
38 8
    }
39
40
41
42
    /**
43
     * Login-page
44
     *
45
     * @throws Exception
46
     *
47
     * @return void
48
     */
49 1
    public function getPostLogin()
50
    {
51 1
        if ($this->userService->checkLoggedin()) {
52 1
            $this->response->redirect("");
53 1
        }
54
55 1
        $title      = "Administration - Login";
56 1
        $form       = new UserLoginForm($this->di);
57
58 1
        $form->check();
59
60
        $data = [
61 1
            "form" => $form->getHTML(),
62 1
        ];
63
64 1
        $this->view->add("user/login", $data);
65
66 1
        $this->pageRender->renderPage(["title" => $title]);
67 1
    }
68
69
70
71
    /**
72
     * Create user page.
73
     *
74
     * @throws Exception
75
     *
76
     * @return void
77
     */
78 1
    public function getPostCreateUser()
79
    {
80 1
        $title      = "Skapa användare";
81 1
        $form       = new CreateUserForm($this->di);
82
83 1
        $form->check();
84
85
        $data = [
86 1
            "content" => $form->getHTML(),
87 1
        ];
88
89 1
        $this->view->add("default2/article", $data);
90
91 1
        $this->pageRender->renderPage(["title" => $title]);
92 1
    }
93
94
95
96
    /**
97
     * Uppdatera användare.
98
     *
99
     * @param integer           $id User id.
100
     *
101
     * @throws Exception
102
     *
103
     * @return void
104
     */
105 1
    public function getPostUpdateUser($id)
106
    {
107 1
        $loggedInUser = $this->userService->getCurrentLoggedInUser();
108
109 1
        if (!$loggedInUser) {
110 1
            $this->response->redirect("user/login");
111 1
            return false;
112
        }
113
114 1
        if ($loggedInUser->id != $id) {
115 1
            if (!$loggedInUser->administrator) {
116 1
                $this->response->redirect("");
117 1
            }
118 1
        }
119
120 1
        $title      = "Uppdatera användaren";
121 1
        $form       = new UpdateUserForm($this->di, $id);
122
123 1
        $form->check();
124
125
        $data = [
126 1
            "content" => $form->getHTML(),
127 1
        ];
128
129 1
        $this->view->add("default2/article", $data);
130
131 1
        $this->pageRender->renderPage(["title" => $title]);
132 1
    }
133
134
135
136
    /**
137
     * Handler with form to delete an item.
138
     *
139
     * @return void
140
     */
141 1
    public function getPostDeleteUser($id)
142
    {
143 1
        $loggedInUser = $this->userService->getCurrentLoggedInUser();
144
145 1
        if ($loggedInUser === null) {
146 1
            $this->response->redirect("user/login");
147 1
        }
148 1
        if ($loggedInUser !== null && !$loggedInUser->administrator) {
149 1
            $this->response->redirect("user/login");
150 1
        }
151
152 1
        $title      = "Radera en användare";
153 1
        $form       = new DeleteUserForm($this->di, $id);
154
155 1
        $form->check();
156
157
        $data = [
158 1
            "content" => $form->getHTML(),
159 1
        ];
160
161 1
        $this->view->add("default2/article", $data);
162
163 1
        $this->pageRender->renderPage(["title" => $title]);
164 1
    }
165
166
167
168
    /**
169
     * Logout user.
170
     *
171
     * @return void
172
     */
173 1
    public function logout()
174
    {
175 1
        $this->session->delete("user");
176 1
        $this->response->redirect("user/login");
177 1
    }
178
}
179