1 | <?php |
||||
2 | |||||
3 | namespace Pamo\User; |
||||
4 | |||||
5 | use Anax\Commons\ContainerInjectableInterface; |
||||
6 | use Anax\Commons\ContainerInjectableTrait; |
||||
7 | use Pamo\User\HTMLForm\UserLoginForm; |
||||
8 | |||||
9 | /** |
||||
10 | * User controller. |
||||
11 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||||
12 | */ |
||||
13 | class UserController implements ContainerInjectableInterface |
||||
14 | { |
||||
15 | use ContainerInjectableTrait; |
||||
16 | |||||
17 | |||||
18 | |||||
19 | /** |
||||
20 | * The initialize method is optional and will always be called before the |
||||
21 | * target method/action. This is a convienient method where you could |
||||
22 | * setup internal properties that are commonly used by several methods. |
||||
23 | * |
||||
24 | * @return void |
||||
25 | */ |
||||
26 | 12 | public function initialize() : void |
|||
27 | { |
||||
28 | 12 | $this->base = "user"; |
|||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
29 | 12 | $this->title = "Users"; |
|||
0 ignored issues
–
show
|
|||||
30 | 12 | $this->game = $this->di->get("game"); |
|||
0 ignored issues
–
show
|
|||||
31 | 12 | $this->page = $this->di->get("page"); |
|||
0 ignored issues
–
show
|
|||||
32 | 12 | $this->page->add("block/nav-admin", $this->game->getNav()); |
|||
33 | 12 | } |
|||
34 | |||||
35 | |||||
36 | |||||
37 | /** |
||||
38 | * Description. |
||||
39 | * |
||||
40 | * @param datatype $variable Description |
||||
0 ignored issues
–
show
The type
Pamo\User\datatype was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||
41 | * |
||||
42 | * @throws Exception |
||||
43 | * |
||||
44 | * @return object as a response object |
||||
45 | */ |
||||
46 | 1 | public function indexActionGet() : object |
|||
47 | { |
||||
48 | 1 | $this->page->add($this->base . "/crud/view-all", [ |
|||
49 | 1 | "title" => "All Users", |
|||
50 | 1 | "users" => $this->game->user->findAll(), |
|||
51 | 1 | "gravatar" => $this->game->gravatar |
|||
52 | ]); |
||||
53 | |||||
54 | 1 | return $this->page->render([ |
|||
55 | 1 | "title" => $this->title, |
|||
56 | ]); |
||||
57 | } |
||||
58 | |||||
59 | |||||
60 | |||||
61 | /** |
||||
62 | * Description. |
||||
63 | * |
||||
64 | * @param string $username of user |
||||
65 | * |
||||
66 | * |
||||
67 | * @return object as a response object |
||||
68 | */ |
||||
69 | 1 | public function invalidAction() : object |
|||
70 | { |
||||
71 | 1 | $this->page->add($this->base . "/invalid", [ |
|||
72 | 1 | "title" => "Invalid user" |
|||
73 | ]); |
||||
74 | |||||
75 | 1 | return $this->page->render([ |
|||
76 | 1 | "title" => $this->title, |
|||
77 | ]); |
||||
78 | } |
||||
79 | |||||
80 | |||||
81 | |||||
82 | /** |
||||
83 | * Description. |
||||
84 | * |
||||
85 | * @param string $username of user |
||||
86 | * |
||||
87 | * |
||||
88 | * @return object as a response object |
||||
89 | */ |
||||
90 | 1 | public function ineligibleAction() : object |
|||
91 | { |
||||
92 | 1 | $this->page->add($this->base . "/ineligible", [ |
|||
93 | 1 | "title" => "Ineligible" |
|||
94 | ]); |
||||
95 | |||||
96 | 1 | return $this->page->render([ |
|||
97 | 1 | "title" => $this->title, |
|||
98 | ]); |
||||
99 | } |
||||
100 | |||||
101 | |||||
102 | |||||
103 | /** |
||||
104 | * Description. |
||||
105 | * |
||||
106 | * @param string $username of user |
||||
107 | * |
||||
108 | * |
||||
109 | * @return object as a response object |
||||
110 | */ |
||||
111 | 1 | public function profileAction() : object |
|||
112 | { |
||||
113 | 1 | if (!$this->game->activeUser()) { |
|||
114 | 1 | return $this->di->get("response")->redirect("user/login"); |
|||
115 | } |
||||
116 | |||||
117 | 1 | $username = $this->game->activeUser("username"); |
|||
118 | |||||
119 | 1 | $this->page->add($this->base . "/profile", [ |
|||
120 | 1 | "title" => "Profile page for $username", |
|||
121 | 1 | "user" => $this->game->user->findWhere("username = ?", $username) |
|||
122 | ]); |
||||
123 | |||||
124 | 1 | return $this->page->render([ |
|||
125 | 1 | "title" => $this->title, |
|||
126 | ]); |
||||
127 | } |
||||
128 | |||||
129 | |||||
130 | |||||
131 | /** |
||||
132 | * Description. |
||||
133 | * |
||||
134 | * @param string $username of user |
||||
135 | * |
||||
136 | * |
||||
137 | * @return object as a response object |
||||
138 | */ |
||||
139 | 1 | public function activityAction($username) : object |
|||
140 | { |
||||
141 | 1 | $select1 = "*, CommentQuestion.id AS commentid, CommentQuestion.text AS comment, CommentQuestion.created AS commentdate, CommentQuestion.user AS commentuser"; |
|||
142 | 1 | $select2 = "*, CommentAnswer.id AS commentid, CommentAnswer.text AS comment, CommentAnswer.created AS commentdate, CommentAnswer.user AS commentuser"; |
|||
143 | |||||
144 | $data = [ |
||||
145 | 1 | "title" => "Activity page for $username", |
|||
146 | 1 | "user" => $this->game->user->findWhere("username = ?", $username), |
|||
147 | 1 | "questions" => $this->game->question->findAllWhere("user = ?", $username), |
|||
148 | 1 | "answers" => $this->game->answer->findAllWhere("user = ?", $username), |
|||
149 | 1 | "commentQuestions" => $this->game->commentQuestion->joinWhere("CommentQuestion.user = ?", $username, "Question", "Question.id = CommentQuestion.questionid", $select1), |
|||
150 | 1 | "commentAnswers" => $this->game->commentAnswer->joinWhere("CommentAnswer.user = ?", $username, "Answer", "Answer.id = CommentAnswer.answerid", $select2), |
|||
151 | 1 | "gravatar" => $this->game->gravatar |
|||
152 | ]; |
||||
153 | |||||
154 | 1 | $this->page->add($this->base . "/activity/header", $data); |
|||
155 | 1 | $this->page->add($this->base . "/activity/questions", $data); |
|||
156 | 1 | $this->page->add($this->base . "/activity/answers", $data); |
|||
157 | 1 | $this->page->add($this->base . "/activity/question-comments", $data); |
|||
158 | 1 | $this->page->add($this->base . "/activity/answer-comments", $data); |
|||
159 | |||||
160 | 1 | return $this->page->render([ |
|||
161 | 1 | "title" => $this->title, |
|||
162 | ]); |
||||
163 | } |
||||
164 | |||||
165 | |||||
166 | |||||
167 | /** |
||||
168 | * Description. |
||||
169 | * |
||||
170 | * @param datatype $variable Description |
||||
171 | * |
||||
172 | * @throws Exception |
||||
173 | * |
||||
174 | * @return object as a response object |
||||
175 | */ |
||||
176 | 1 | public function loginAction() : object |
|||
177 | { |
||||
178 | 1 | $form = new UserLoginForm($this->di); |
|||
179 | 1 | $form->check(); |
|||
180 | |||||
181 | 1 | $this->page->add("block/header", [ |
|||
182 | 1 | "title" => "Login", |
|||
183 | ]); |
||||
184 | |||||
185 | 1 | $this->page->add("anax/v2/article/default", [ |
|||
186 | 1 | "content" => $form->getHTML(), |
|||
187 | ]); |
||||
188 | |||||
189 | 1 | return $this->page->render([ |
|||
190 | 1 | "title" => "A login page", |
|||
191 | ]); |
||||
192 | } |
||||
193 | |||||
194 | |||||
195 | |||||
196 | /** |
||||
197 | * Description. |
||||
198 | * |
||||
199 | * @param datatype $variable Description |
||||
200 | * |
||||
201 | * @throws Exception |
||||
202 | * |
||||
203 | * @return object as a response object |
||||
204 | */ |
||||
205 | 1 | public function createAction() : object |
|||
206 | { |
||||
207 | 1 | $form = $this->game->getUserForm("create"); |
|||
208 | 1 | $form->check(); |
|||
209 | |||||
210 | 1 | $this->page->add("block/header", [ |
|||
211 | 1 | "title" => "Register", |
|||
212 | ]); |
||||
213 | |||||
214 | 1 | $this->page->add("anax/v2/article/default", [ |
|||
215 | 1 | "content" => $form->getHTML(), |
|||
216 | ]); |
||||
217 | |||||
218 | 1 | return $this->page->render([ |
|||
219 | 1 | "title" => "A create user page", |
|||
220 | ]); |
||||
221 | } |
||||
222 | |||||
223 | |||||
224 | |||||
225 | /** |
||||
226 | * Description. |
||||
227 | * |
||||
228 | * @return object as a response object |
||||
229 | */ |
||||
230 | 1 | public function updateAction() : object |
|||
231 | { |
||||
232 | 1 | if (!$this->game->activeUser()) { |
|||
233 | 1 | return $this->di->get("response")->redirect("user/login"); |
|||
234 | } |
||||
235 | |||||
236 | 1 | $username = $this->game->activeUser("username"); |
|||
237 | |||||
238 | 1 | $form = $this->game->getUserForm("edit", $username); |
|||
239 | 1 | $form->check(); |
|||
240 | |||||
241 | 1 | $this->page->add("block/header", [ |
|||
242 | 1 | "title" => "Update Profile", |
|||
243 | ]); |
||||
244 | |||||
245 | 1 | $this->page->add("anax/v2/article/default", [ |
|||
246 | 1 | "content" => $form->getHTML(), |
|||
247 | ]); |
||||
248 | |||||
249 | 1 | return $this->page->render([ |
|||
250 | 1 | "title" => "Update profile", |
|||
251 | ]); |
||||
252 | } |
||||
253 | |||||
254 | |||||
255 | |||||
256 | /** |
||||
257 | * Description. |
||||
258 | * |
||||
259 | * @return object as a response object |
||||
260 | */ |
||||
261 | 1 | public function deleteAction() : object |
|||
262 | { |
||||
263 | 1 | if (!$this->game->activeUser()) { |
|||
264 | 1 | return $this->di->get("response")->redirect("user/login"); |
|||
265 | } |
||||
266 | |||||
267 | 1 | $username = $this->game->activeUser("username"); |
|||
268 | |||||
269 | 1 | $form = $this->game->getUserForm("delete", $username); |
|||
270 | 1 | $form->check(); |
|||
271 | |||||
272 | 1 | $this->page->add("block/header", [ |
|||
273 | 1 | "title" => "Delete Account", |
|||
274 | ]); |
||||
275 | |||||
276 | 1 | $this->page->add("anax/v2/article/default", [ |
|||
277 | 1 | "content" => $form->getHTML(), |
|||
278 | ]); |
||||
279 | |||||
280 | 1 | return $this->page->render([ |
|||
281 | 1 | "title" => "Delete profile", |
|||
282 | ]); |
||||
283 | } |
||||
284 | |||||
285 | |||||
286 | |||||
287 | /** |
||||
288 | * Description. |
||||
289 | * |
||||
290 | * @return null |
||||
291 | */ |
||||
292 | 1 | public function logoutAction() : object |
|||
293 | { |
||||
294 | 1 | $this->di->get("session")->set("user", null); |
|||
295 | |||||
296 | 1 | return $this->di->get("response")->redirect("user/login")->send(); |
|||
297 | } |
||||
298 | |||||
299 | /** |
||||
300 | * This sample method dumps the content of $di. |
||||
301 | * GET mountpoint/dump-app |
||||
302 | * |
||||
303 | * @return string |
||||
304 | */ |
||||
305 | 1 | public function dumpDiActionGet() : string |
|||
306 | { |
||||
307 | // Deal with the action and return a response. |
||||
308 | 1 | $services = implode(", ", $this->di->getServices()); |
|||
309 | 1 | return __METHOD__ . "<p>\$di contains: $services"; |
|||
310 | } |
||||
311 | |||||
312 | |||||
313 | |||||
314 | /** |
||||
315 | * Adding an optional catchAll() method will catch all actions sent to the |
||||
316 | * router. You can then reply with an actual response or return void to |
||||
317 | * allow for the router to move on to next handler. |
||||
318 | * A catchAll() handles the following, if a specific action method is not |
||||
319 | * created: |
||||
320 | * ANY METHOD mountpoint/** |
||||
321 | * |
||||
322 | * @param array $args as a variadic parameter. |
||||
323 | * |
||||
324 | * @return mixed |
||||
325 | * |
||||
326 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||||
327 | */ |
||||
328 | 1 | public function catchAll(...$args) |
|||
0 ignored issues
–
show
The parameter
$args is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
329 | { |
||||
330 | // Deal with the request and send an actual response, or not. |
||||
331 | //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args); |
||||
332 | 1 | return; |
|||
333 | } |
||||
334 | } |
||||
335 |