DeleteCommentForm   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 26.74 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 73.81%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 23
loc 86
ccs 31
cts 42
cp 0.7381
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A callbackSubmit() 0 10 1
A __construct() 23 23 1
B getAllItems() 0 28 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Marcusgsta\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Marcusgsta\Comment\Comment;
8
9
/**
10
 * Form to delete an item.
11
 */
12
class DeleteCommentForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     */
19 1 View Code Duplication
    public function __construct(DIInterface $di)
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...
20
    {
21 1
        parent::__construct($di);
22 1
        $this->form->create(
23
            [
24 1
                "id" => __CLASS__,
25 1
                "legend" => "Delete an item",
26 1
            ],
27
            [
28
                "select" => [
29 1
                    "type"        => "select",
30 1
                    "label"       => "Select item to delete:",
31 1
                    "options"     => $this->getAllItems(),
32 1
                ],
33
34
                "submit" => [
35 1
                    "type" => "submit",
36 1
                    "value" => "Delete item",
37 1
                    "callback" => [$this, "callbackSubmit"]
38 1
                ],
39
            ]
40 1
        );
41 1
    }
42
43
44
45
    /**
46
     * Get all items as array suitable for display in select option dropdown.
47
     *
48
     * @return array with key value of all items.
49
     */
50 1
    protected function getAllItems()
51
    {
52 1
        $comment = new Comment();
53 1
        $comment->setDb($this->di->get("db"));
54 1
        $allComments = $comment->findAll();
55
56
        // filter array of comments to current user
57 1
        $newArray = array_filter($allComments, function ($obj) {
58 1
            $acronym = $this->di->session->get("user");
59 1
            $role = $this->di->get("commentController")->getRole($acronym);
60
            // if user is not administrator ...
61 1
            if ($role != 10) {
62
                // filter out user's comment
63 1
                if ($obj->acronym != $acronym) {
64 1
                    return false;
65
                }
66
                return true;
67
            }
68
            return true;
69 1
        });
70
71 1
        $comments = ["-1" => "Select an item..."];
72 1
        foreach ($newArray as $obj) {
73
            $comments[$obj->id] = "{$obj->commenttext} ({$obj->id})";
74 1
        }
75
76 1
        return $comments;
77
    }
78
79
80
81
    /**
82
     * Callback for submit-button which should return true if it could
83
     * carry out its work and false if something failed.
84
     *
85
     * @return boolean true if okey, false if something went wrong.
86
     */
87
    public function callbackSubmit()
88
    {
89
        $comment = new Comment();
90
        $comment->setDb($this->di->get("db"));
91
        $comment->find("id", $this->form->value("select"));
92
        $comment->delete();
93
94
        $this->form->addOutput("Kommentaren raderades.");
95
        $this->di->get("response")->redirect("comment/delete");
96
    }
97
}
98