Test Failed
Push — master ( 72a473...678733 )
by Gunvor
01:37
created

DeleteCommForm::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 21
cts 21
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Anax\Comments\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Anax\Comments\Comm;
8
9
/**
10
 * Form to delete an item.
11
 */
12
class DeleteCommForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 1
    public function __construct(DIInterface $di, $commid)
20
    {
21 1
        parent::__construct($di);
22 1
        $comm = $this->getCommDetails($commid);
23
24 1
        $this->form->create(
25
            [
26 1
                "id" => __CLASS__,
27 1
                "legend" => "Alla kommentarer tas också bort",
28 1
            ],
29
            [
30
                "userid" => [
31 1
                    "type"        => "hidden",
32 1
                    "value"       => $comm->userid,
33 1
                ],
34
35
                "id" => [
36 1
                    "type"        => "text",
37 1
                    "readonly"    => true,
38 1
                    "value"       => $commid,
39 1
                    "label"       => "Ta bort inlägget",
40 1
                ],
41
42
                "submit" => [
43 1
                    "type" => "submit",
44 1
                    "value" => "Ta bort",
45 1
                    "callback" => [$this, "callbackSubmit"]
46 1
                ],
47
            ]
48 1
        );
49 1
    }
50
51
52
53
    /**
54
     * Get all items as array suitable for display in select option dropdown.
55
     *
56
     * @return array with key value of all items.
57
     */
58 1 View Code Duplication
    protected function getCommDetails($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60 1
        $comm = new Comm();
61 1
        $comm->setDb($this->di->get("db"));
62 1
        $comm->find("id", $id);
63 1
        return $comm;
64
    }
65
66
67
68
    /**
69
     * Callback for submit-button which should return true if it could
70
     * carry out its work and false if something failed.
71
     *
72
     * @return boolean true if okey, false if something went wrong.
73
     */
74
    public function callbackSubmit()
75
    {
76
        $comm = new Comm();
77
        $comm->setDb($this->di->get("db"));
78
79
        $all = $comm->findAll();
80
        $objects = "";
81
82
        foreach ($all as $obj) {
83
            if ($obj->parentid && $obj->parentid == $this->form->value("id")) {
84
                $comm->find("id", $obj->id);
85
                $objects .= $comm->title . ", ";
86
                $comm->delete();
87
            }
88
        }
89
90
        $comm->find("id", $this->form->value("id"));
91
        $objects .= $comm->title;
92
        $comm->delete();
93
        $pagerender = $this->di->get("pageRender");
94
        $pagerender->redirect("comm");
95
    }
96
}
97