GameController   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 75
dl 0
loc 171
ccs 78
cts 78
cp 1
rs 10
c 0
b 0
f 0
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
C questionAction() 0 67 13
A voteAction() 0 13 3
A dumpDiActionGet() 0 5 1
A initialize() 0 18 1
A catchAll() 0 5 1
1
<?php
2
3
namespace Pamo\Game;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * Game controller
10
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
11
 * @SuppressWarnings(PHPMD.NPathComplexity)
12
 */
13
class GameController 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 10
    public function initialize() : void
27
    {
28 10
        $this->base = "game";
0 ignored issues
show
Bug Best Practice introduced by
The property base does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29 10
        $this->title = "Q & A";
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30 10
        $this->game = $this->di->get("game");
0 ignored issues
show
Bug Best Practice introduced by
The property game does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31 10
        $this->sortBy = $this->game->getASort();
0 ignored issues
show
Bug Best Practice introduced by
The property sortBy does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32 10
        $this->sortType = $this->game->getASortType();
0 ignored issues
show
Bug Best Practice introduced by
The property sortType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
34 10
        $this->data = [
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35 10
            "textFilter" => $this->di->get("textfilter"),
36 10
            "gravatar" => $this->di->get("gravatar"),
37 10
            "user" => $this->game->user,
38 10
            "activeSort" => "$this->sortBy $this->sortType"
39
        ];
40
41 10
        $this->page = $this->di->get("page");
0 ignored issues
show
Bug Best Practice introduced by
The property page does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42 10
        $this->page->add("block/nav-admin", $this->game->getNav());
43 10
        $this->request = $this->di->get("request");
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44 10
    }
45
46
47
48
    /**
49
     * Show questions, answers and comments.
50
     *
51
     * @return object as a response object
52
     */
53 7
    public function questionAction(int $questionId) : object
54
    {
55 7
        $data = array_merge([
56 7
            "question" => $this->game->question->findById($questionId),
57 7
            "answers" => $this->game->answer->findAllWhereOrder("questionid = ?", $questionId, "$this->sortBy $this->sortType"),
58 7
            "commentQuestion" => $this->game->commentQuestion->findAllWhere("questionid = ?", $questionId),
59 7
            "commentAnswer" => $this->game->commentAnswer,
60 7
            "tags" => $this->game->tagQuestion->findAllWhere("questionid = ?", $questionId)
61 7
        ], $this->data);
62
63 7
        if ($this->game->activeUser()) {
64 7
            $request = $this->di->get("request");
65 7
            $admin = $request->getGet("admin", null);
66 7
            $adminType = $request->getGet("adminType", null);
67 7
            $adminId = $request->getGet("adminId", null);
68
69 7
            $answerForm = $this->game->getAnswerForm("create", $questionId);
70 7
            $answerForm->check();
71
72 7
            switch ($admin) {
73 7
                case "question":
74 3
                    if ($adminType === "create") {
75 1
                        return $this->di->get("response")->redirect("question/create")->send();
76
                    }
77 3
                    $owner = $this->game->question->findById($questionId)->user;
78 3
                    $adminForm = $this->game->getQuestionForm($adminType, $questionId);
79 3
                    break;
80 5
                case "answer":
81 2
                    $owner = $this->game->answer->findById($adminId)->user;
82 2
                    $adminForm = $this->game->getAnswerForm($adminType, $questionId);
83 2
                    break;
84 4
                case "comment-question":
85 2
                    $owner = $this->game->commentQuestion->findById($adminId)->user;
86 2
                    $adminForm = $this->game->getCommentForm($adminType, $questionId, "question");
87 2
                    break;
88 3
                case "comment-answer":
89 2
                    $owner = $this->game->commentAnswer->findById($adminId)->user;
90 2
                    $adminForm = $this->game->getCommentForm($adminType, $questionId, "answer");
91 2
                    break;
92
            }
93
94 7
            if (isset($owner) && !$this->game->validUser($owner)) {
95 2
                return $this->di->get("response")->redirect("user/invalid")->send();
96
            }
97
98 6
            if (isset($adminForm)) {
99 4
                $adminForm->check();
100
            }
101
102 6
            $data["answerForm"] = $answerForm->getHTML($this->game->getFormSettings());
103 6
            $data["admin"] = $admin;
104 6
            $data["adminForm"] = isset($adminForm) ? $adminForm->getHTML($this->game->getFormSettings()) : null;
105 6
            $data["adminAnswerId"] = $admin === "answer" ? $request->getGet("adminId") : $request->getGet("answerId", null);
106
        }
107
108 6
        if ($this->game->activeUser()) {
109 6
            $this->page->add($this->base . "/block/question-admin", $data);
110 6
            $this->page->add($this->base . "/block/answer-sort", $data);
111 6
            $this->page->add($this->base . "/block/answer-admin", $data);
112
        } else {
113 1
            $this->page->add($this->base . "/block/question-no-admin", $data);
114 1
            $this->page->add($this->base . "/block/answer-sort", $data);
115 1
            $this->page->add($this->base . "/block/answer-no-admin", $data);
116
        }
117
118 6
        return $this->page->render([
119 6
            "title" => $this->title,
120
        ]);
121
    }
122
123
124
125
    /**
126
     * Handler with form to update an item.
127
     *
128
     * @param int $id the id to update.
129
     * @param string $vote the answer up or down 1.
130
     *
131
     * @return null as a response object
132
     */
133 1
    public function voteAction(string $type, int $id, string $vote)
134
    {
135 1
        if (!$this->game->activeUser()) {
136 1
            return $this->di->get("response")->redirect("user/login");
137
        }
138
139 1
        $res = $this->game->vote($type, $id, $vote);
140
141 1
        if (!$res) {
142 1
            return $this->di->get("response")->redirect("user/ineligible");
143
        }
144
145 1
        return $this->di->get("response")->redirect("game/question/$res");
146
    }
147
148
149
150
    /**
151
     * This sample method dumps the content of $di.
152
     * GET mountpoint/dump-app
153
     *
154
     * @return string
155
     */
156 1
    public function dumpDiActionGet() : string
157
    {
158
        // Deal with the action and return a response.
159 1
        $services = implode(", ", $this->di->getServices());
160 1
        return __METHOD__ . "<p>\$di contains: $services";
161
    }
162
163
164
165
    /**
166
     * Adding an optional catchAll() method will catch all actions sent to the
167
     * router. You can then reply with an actual response or return void to
168
     * allow for the router to move on to next handler.
169
     * A catchAll() handles the following, if a specific action method is not
170
     * created:
171
     * ANY METHOD mountpoint/**
172
     *
173
     * @param array $args as a variadic parameter.
174
     *
175
     * @return mixed
176
     *
177
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
178
     */
179 1
    public function catchAll(...$args)
0 ignored issues
show
Unused Code introduced by
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 ignore-unused  annotation

179
    public function catchAll(/** @scrutinizer ignore-unused */ ...$args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
    {
181
        // Deal with the request and send an actual response, or not.
182
        //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args);
183 1
        return;
184
    }
185
}
186