CommentController::getPostCreateComment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 17
loc 17
c 0
b 0
f 0
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Marcusgsta\Comment;
4
5
use \Anax\Configure\ConfigureInterface;
6
use \Anax\Configure\ConfigureTrait;
7
use \Anax\DI\InjectionAwareInterface;
8
use \Anax\DI\InjectionAwareTrait;
9
use \Marcusgsta\Comment\HTMLForm\EditCommentForm;
10
use \Marcusgsta\Comment\HTMLForm\CreateCommentForm;
11
use \Marcusgsta\Comment\HTMLForm\DeleteCommentForm;
12
13
/**
14
 * A controller class.
15
 */
16
class CommentController implements
17
    ConfigureInterface,
18
    InjectionAwareInterface
19
{
20
    use ConfigureTrait,
21
        InjectionAwareTrait;
22
23
24
25
    /**
26
     * @var $data description
27
     */
28
    //private $data;
29
30
31
32
    /**
33
     * Description.
34
     *
35
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     *
37
     * @throws Exception
38
     *
39
     * @return void
40
     */
41 1
    public function getComments($test = "not_test")
42
    {
43
        //$route = $this->di->request->getRoute();
44
45 1
        $comment = new Comment();
46 1
        $comment->setDb($this->di->get("db"));
47
48 1
        $allComments = $comment->findAll();
49
50
        // filter array of comments to current page
51 1
        $newArray = array_filter($allComments, function ($obj) use ($test) {
52
53 1
            $route = $this->di->get("request")->getRoute();
54 1
            $route = empty($route) ? "index" : $route;
55
56
            // for testing purposes
57 1
            if ($test == "test") {
58 1
                $route = "index";
59 1
            }
60
61 1
            if ($obj->page != $route) {
62 1
                return false;
63
            }
64 1
            return true;
65 1
        });
66
67
        $data = [
68 1
            "items" => $newArray,
69 1
        ];
70 1
        return $data;
71
    }
72
73
    /**
74
     * Description.
75
     *
76
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
77
     *
78
     * @throws Exception
79
     *
80
     * @return void
81
     */
82 View Code Duplication
    public function getPostCreateComment()
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...
83
    {
84
        $title      = "A create comment page";
85
        $view       = $this->di->get("view");
86
        $pageRender = $this->di->get("pageRender");
87
        $form       = new CreateCommentForm($this->di);
88
89
        $form->check();
90
91
        $data = [
92
            "content" => $form->getHTML(),
93
        ];
94
95
        $view->add("default2/article", $data);
96
97
        $pageRender->renderPage(["title" => $title]);
98
    }
99
100
    /**
101
     * Description.
102
     *
103
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
     *
105
     * @throws Exception
106
     *
107
     * @return void
108
     */
109
    public function getPostEditComment($key, $itemId)
110
    {
111
        $data = ["page" => $key, "commentid" => $itemId];
112
113
        $commentId = $data['commentid'];
114
        $loggedInUser = $this->di->session->get("user");
115
        $role = $this->getRole($loggedInUser);
116
117
        if ($role != 10) {
118
            if (!$this->isEditable($commentId, $loggedInUser)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->isEditable($commentId, $loggedInUser) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
119
                echo "Du har inte tillgång till att redigera denna kommentar.";
120
                return false;
121
            }
122
        }
123
124
        $title      = "An edit comment page";
125
        $view       = $this->di->get("view");
126
        $pageRender = $this->di->get("pageRender");
127
        $form       = new EditCommentForm($this->di, $data);
128
129
        $form->check();
130
131
        $data = [
132
            "content" => $form->getHTML(),
133
        ];
134
135
        $view->add("default2/article", $data);
136
137
        $pageRender->renderPage(["title" => $title]);
138
    }
139
140
    /**
141
     * Description.
142
     *
143
     * @param datatype $variable Description
0 ignored issues
show
Bug introduced by
There is no parameter named $variable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
144
     *
145
     * @throws Exception
146
     *
147
     * @return void
148
     */
149 View Code Duplication
    public function deleteComment()
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...
150
    {
151
        $title      = "A delete comments page";
152
        $view       = $this->di->get("view");
153
        $pageRender = $this->di->get("pageRender");
154
        $form       = new DeleteCommentForm($this->di);
155
156
        $form->check();
157
158
        $data = [
159
            "content" => $form->getHTML(),
160
        ];
161
162
        $view->add("default2/article", $data);
163
164
        $pageRender->renderPage(["title" => $title]);
165
    }
166
167
    /**
168
    * get a user's role
169
    * @param string user's acronym
170
    *
171
    * @return integer user's role
172
    **/
173 2
    public function getRole($acronym)
174
    {
175 2
        $user = new \Anax\User\User();
176 2
        $user->setDb($this->di->get("db"));
177 2
        $user->find("acronym", $acronym);
178 2
        $role = isset($user->role) ? $user->role : null;
0 ignored issues
show
Bug introduced by
The property role does not seem to exist in Anax\User\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
179 2
        return $role;
180
    }
181
182
    /**
183
    * check if logged in user can edit the comment
184
    * @param string commentid
185
    *
186
    * @return bool
187
    **/
188
    public function isEditable($commentId, $user)
189
    {
190
        $comment = new Comment;
191
        $comment->setDb($this->di->get("db"));
192
        $comment = $comment->find("id", $commentId);
193
194
        if ($comment) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $comment of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
195
            if ($comment->acronym != $user) {
196
                return false;
197
            }
198
            return true;
199
        }
200
    }
201
}
202