CreateForm   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 53.19%

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 108
ccs 25
cts 47
cp 0.5319
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 41 1
A getAllItems() 0 11 2
A callbackSubmit() 0 14 1
A callbackSuccess() 0 10 1
1
<?php
2
3
namespace Seb\Questions\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Seb\Questions\Questions;
8
use Seb\Tags\Tags;
9
use Seb\User\User;
10
11
/**
12
 * Form to create an item.
13
 */
14
class CreateForm 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 Seb\Questions\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 2
    public function __construct(ContainerInterface $di)
22
    {
23 2
        parent::__construct($di);
24 2
        $this->form->create(
25
            [
26 2
                "id" => __CLASS__,
27
                "legend" => "Details of the item",
28
            ],
29
            [
30
                "topic" => [
31 2
                    "type" => "text",
32
                    "validation" => ["not_empty"],
33
                ],
34
35
                "question" => [
36
                    "type" => "textarea",
37
                    "validation" => ["not_empty"],
38
                ],
39
40
                "tag1" => [
41 2
                    "type" => "select",
42 2
                    "label"       => "tag1",
43 2
                    "options"     => $this->getAllItems(),
44
                ],
45
46
                "tag2" => [
47 2
                    "type" => "select",
48 2
                    "label"       => "tag2",
49 2
                    "options"     => $this->getAllItems(),
50
                ],
51
52
                "tag3" => [
53 2
                    "type" => "select",
54 2
                    "label"       => "tag3",
55 2
                    "options"     => $this->getAllItems(),
56
                ],
57
58
                "submit" => [
59 2
                    "type" => "submit",
60 2
                    "value" => "Create item",
61 2
                    "callback" => [$this, "callbackSubmit"]
62
                ],
63
            ]
64
        );
65 2
    }
66
67
    /**
68
     * Get all items as array suitable for display in select option dropdown.
69
     *
70
     * @return array with key value of all items.
71
     */
72 2
    protected function getAllItems() : array
73
    {
74 2
        $tags = new Tags();
75 2
        $tags->setDb($this->di->get("dbqb"));
76
77 2
        $items = ["" => "Select an item..."];
78 2
        foreach ($tags->findAll() as $obj) {
79 2
            $items[$obj->tag] = "{$obj->tag}";
80
        }
81
82 2
        return $items;
83
    }
84
85
    /**
86
     * Callback for submit-button which should return true if it could
87
     * carry out its work and false if something failed.
88
     *
89
     * @return bool true if okey, false if something went wrong.
90
     */
91
    public function callbackSubmit() : bool
92
    {
93
        $session = $this->di->get("session");
94
        $questions = new Questions();
95
        $questions->setDb($this->di->get("dbqb"));
96
        $questions->acronym = $session->get("acronym");
97
        $questions->userid = $session->get("userid");
98
        $questions->topic = $this->form->value("topic");
99
        $questions->question = $this->form->value("question");
100
        $questions->tag1 = $this->form->value("tag1");
101
        $questions->tag2 = $this->form->value("tag2");
102
        $questions->tag3 = $this->form->value("tag3");
103
        $questions->save();
104
        return true;
105
    }
106
107
    /**
108
     * Callback what to do if the form was successfully submitted, this
109
     * happen when the submit callback method returns true. This method
110
     * can/should be implemented by the subclass for a different behaviour.
111
     */
112
    public function callbackSuccess()
113
    {
114
        $session = $this->di->get("session");
115
        $userid = $session->get("userid");
116
        $user = new User();
117
        $user->setDb($this->di->get("dbqb"));
118
        $user->find("id", $userid);
119
        $user->score += 1;
120
        $user->save();
121
        $this->di->get("response")->redirect("forum")->send();
122
    }
123
}
124