CreateQuestionForm   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 115
c 0
b 0
f 0
ccs 44
cts 44
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 34 1
A callbackSubmit() 0 25 3
A callbackSuccess() 0 3 1
A addTag() 0 17 2
1
<?php
2
3
namespace Pamo\Question\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Pamo\Question\Question;
8
use Pamo\Tag\Tag;
9
use Pamo\Tag\TagQuestion;
10
11
/**
12
 * Form to create an item.
13
 */
14
class CreateQuestionForm extends FormModel
15
{
16
    /**
17
     * Constructor injects with DI container.
18
     *
19
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Pamo\Question\HTMLForm\P...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
20
     */
21 1
    public function __construct(ContainerInterface $di)
22
    {
23 1
        parent::__construct($di);
24 1
        $this->user = $di->get("session")->get("user", null);
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25 1
        $this->form->create(
26
            [
27 1
                "id" => __CLASS__,
28
                "legend" => "Details of the item",
29
            ],
30
            [
31 1
                "user" => [
32 1
                    "type" => "hidden",
33
                    "validation" => ["not_empty"],
34 1
                    "value" => $this->user["username"],
35
                ],
36
37
                "title" => [
38
                    "type" => "text",
39
                    "validation" => ["not_empty"],
40
                ],
41
42
                "question" => [
43
                    "type" => "text",
44
                    "validation" => ["not_empty"],
45
                ],
46
47
                "tags" => [
48
                    "type" => "text"
49
                ],
50
51
                "submit" => [
52 1
                    "type" => "submit",
53 1
                    "value" => "Submit",
54 1
                    "callback" => [$this, "callbackSubmit"]
55
                ],
56
            ]
57
        );
58 1
    }
59
60
61
62
    /**
63
     * Callback for submit-button which should return true if it could
64
     * carry out its work and false if something failed.
65
     *
66
     * @return bool true if okey, false if something went wrong.
67
     */
68 1
    public function callbackSubmit() : bool
69
    {
70 1
        $question = new Question();
71 1
        $question->setDb($this->di->get("dbqb"));
72 1
        $question->title = $this->form->value("title");
73 1
        $question->text = $this->form->value("question");
74 1
        $question->user = $this->form->value("user");
75 1
        $question->vote = 0;
76 1
        $question->save();
77
78 1
        $tags = $this->form->value("tags");
79
80 1
        if ($tags) {
81 1
            $questionTag = explode(",", $tags);
82 1
            foreach ($questionTag as $formTag) {
83 1
                $this->addTag($formTag, $question->id);
84
            }
85
        } else {
86 1
            $this->addTag("General", $question->id);
87
        }
88
89 1
        $game = $this->di->get("game");
90 1
        $game->updateRank($question->user, 5);
91
92 1
        return true;
93
    }
94
95
96
97
    /**
98
     * Add a new tag.
99
     */
100 1
    public function addTag($tagName, $question)
101
    {
102 1
        $tag = new Tag();
103 1
        $tag->setDb($this->di->get("dbqb"));
104 1
        $tagQuestion = new TagQuestion();
105 1
        $tagQuestion->setDb($this->di->get("dbqb"));
106 1
        $match = $tag->findAllWhere("name = ?", $tagName);
107
108 1
        if (count($match) == 0) {
109 1
            $tag->name = $tagName;
110 1
            $tag->save();
111
        }
112
113 1
        $tagQuestion->questionid = $question;
114 1
        $tagQuestion->tagname = $tagName;
115 1
        $tagQuestion->save();
116 1
        return true;
117
    }
118
119
120
121
    /**
122
     * Callback what to do if the form was successfully submitted, this
123
     * happen when the submit callback method returns true. This method
124
     * can/should be implemented by the subclass for a different behaviour.
125
     */
126 1
    public function callbackSuccess()
127
    {
128 1
        $this->di->get("response")->redirect("question")->send();
129 1
    }
130
131
132
133
    // /**
134
    //  * Callback what to do if the form was unsuccessfully submitted, this
135
    //  * happen when the submit callback method returns false or if validation
136
    //  * fails. This method can/should be implemented by the subclass for a
137
    //  * different behaviour.
138
    //  */
139
    // public function callbackFail()
140
    // {
141
    //     $this->di->get("response")->redirectSelf()->send();
142
    // }
143
}
144