DeleteForm::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 19
ccs 0
cts 10
cp 0
crap 2
rs 9.8666
1
<?php
2
3
namespace Aiur18\Question\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Aiur18\Question\Question;
8
use Aiur18\getset\getset;
9
10
/**
11
 * Form to delete an item.
12
 */
13
class DeleteForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container.
17
     *
18
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Aiur18\Question\HTMLForm...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
19
     */
20
    public function __construct(ContainerInterface $di)
21
    {
22
        parent::__construct($di);
23
        $this->form->create(
24
            [
25
                "id" => __CLASS__,
26
                "legend" => "Delete an item",
27
            ],
28
            [
29
                "select" => [
30
                    "type"        => "select",
31
                    "label"       => "Select item to delete:",
32
                    "options"     => $this->getAllItems(),
33
                ],
34
35
                "submit" => [
36
                    "type" => "submit",
37
                    "value" => "Delete item",
38
                    "callback" => [$this, "callbackSubmit"]
39
                ],
40
            ]
41
        );
42
    }
43
44
45
46
    /** Method
47
     */
48
    protected function getAllItems() : array
49
    {
50
        $question = new Question();
51
        $question->setDb($this->di->get("dbqb"));
52
53
        $questions = ["-1" => "Select an item..."];
54
        foreach ($question->findAll() as $obj) {
55
            $questions[$obj->id] = "{$obj->column1} ({$obj->id})";
56
        }
57
58
        return $questions;
59
    }
60
61
62
63
    /** Method
64
     */
65
    public function callbackSubmit() : bool
66
    {
67
        $question = new Question();
68
        $question->setDb($this->di->get("dbqb"));
69
        $question->find("id", $this->form->value("select"));
70
        $question->delete();
71
        return true;
72
    }
73
74
75
76
    /** Method
77
     */
78
    public function callbackSuccess()
79
    {
80
        $this->di->get("response")->redirect("question")->send();
81
    }
82
}
83