CreateAnswersForm   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 36.67%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 74
ccs 11
cts 30
cp 0.3667
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 27 1
A callbackSubmit() 0 11 1
A callbackSuccess() 0 10 1
1
<?php
2
3
namespace Seb\Answers\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Seb\Answers\Answers;
8
use Seb\User\User;
9
10
/**
11
 * Form to create an item.
12
 */
13
class CreateAnswersForm extends FormModel
14
{
15
    public $qid;
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\Answers\HTMLForm\Psr...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, int $id)
22
    {
23 2
        $this->qid = $id;
24 2
        parent::__construct($di);
25 2
        $this->form->create(
26
            [
27 2
                "id" => __CLASS__,
28
                "legend" => "Create Answer",
29
            ],
30
            [
31
                
32
                "questionid" => [
33 2
                    "type" => "hidden",
34
                    "validation" => ["not_empty"],
35
                    "readonly" => true,
36 2
                    "value" => $id,
37
                ],
38
39
                "answer" => [
40
                    "type" => "textarea",
41
                    "validation" => ["not_empty"],
42
                ],
43
44
                "submit" => [
45 2
                    "type" => "submit",
46 2
                    "value" => "Create item",
47 2
                    "callback" => [$this, "callbackSubmit"]
48
                ],
49
            ]
50
        );
51 2
    }
52
53
    /**
54
     * Callback for submit-button which should return true if it could
55
     * carry out its work and false if something failed.
56
     *
57
     * @return bool true if okey, false if something went wrong.
58
     */
59
    public function callbackSubmit() : bool
60
    {
61
        $session = $this->di->get("session");
62
        $answers = new Answers();
63
        $answers->setDb($this->di->get("dbqb"));
64
        $answers->acronym = $session->get("acronym");
65
        $answers->userid = $session->get("userid");
66
        $answers->answer = $this->form->value("answer");
67
        $answers->questionid = $this->form->value("questionid");
68
        $answers->save();
69
        return true;
70
    }
71
72
    /**
73
     * Callback what to do if the form was successfully submitted, this
74
     * happen when the submit callback method returns true. This method
75
     * can/should be implemented by the subclass for a different behaviour.
76
     */
77
    public function callbackSuccess()
78
    {
79
        $session = $this->di->get("session");
80
        $userid = $session->get("userid");
81
        $user = new User();
82
        $user->setDb($this->di->get("dbqb"));
83
        $user->find("id", $userid);
84
        $user->score += 1;
85
        $user->save();
86
        $this->di->get("response")->redirect("forum/topic/{$this->qid}")->send();
87
    }
88
}
89