EditPostForm::callbackSubmit()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 26
loc 26
ccs 0
cts 16
cp 0
rs 8.5806
c 1
b 0
f 0
cc 4
eloc 16
nc 4
nop 0
crap 20
1
<?php
2
3
namespace Nicklas\Comment\HTMLForm\Comment;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Nicklas\Comment\Modules\User;
8
use \Nicklas\Comment\Modules\Post;
9
10
/**
11
 * Example of FormModel implementation.
12
 */
13 View Code Duplication
class EditPostForm extends FormModel
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * Constructor injects with DI container.
17
     *
18
     * @param Anax\DI\DIInterface $di a service container
19
     */
20
    public function __construct(DIInterface $di, $id)
21
    {
22
        parent::__construct($di);
23
        $this->post     = new Post($di->get("db"));
0 ignored issues
show
Bug introduced by
The property post does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $this->post->find("id", $id);
25
        if ($this->post->id == null) {
26
            $di->get("response")->redirect("question");
27
        }
28
        $this->form->create(
29
            [
30
                "id" => __CLASS__,
31
                "legend" => "Här kan du uppdatera din post"
32
            ],
33
            [
34
                "text" => [
35
                    "type"  => "textarea",
36
                    "value" => $this->post->text,
37
                    "label" => "Här kan du skriva din fråga",
38
                    "placeholder" => "Din fråga"
39
                ],
40
                "submit" => [
41
                    "type" => "submit",
42
                    "value" => "Spara",
43
                    "callback" => [$this, "callbackSubmit"]
44
                ],
45
            ]
46
        );
47
    }
48
49
    /**
50
     * Callback for submit-button which should return true if it could
51
     * carry out its work and false if something failed.
52
     *
53
     * @return boolean true if okey, false if something went wrong.
54
     */
55
    public function callbackSubmit()
56
    {
57
        // Get values from the submitted form
58
        $text = $this->form->value("text");
59
60
        if (!$this->di->get('session')->has("user")) {
61
            $this->form->addOutput("Du behöver logga in");
62
            return false;
63
        }
64
65
        $user = new User($this->di->get("db"));
66
        if ($user->controlAuthority($this->di->get('session')->get("user"), $this->post->user) != true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
67
            $this->form->addOutput("Du får inte redigera denna.");
68
            return false;
69
        }
70
71
        if ($text == "") {
72
            $this->form->addOutput("Du skrev aldrig något. Skriv gärna något.");
73
            return false;
74
        }
75
76
        $this->post->text = $text;
77
        $this->post->save();
78
        $this->form->addOutput("Du har uppdaterat inlägget");
79
        return true;
80
    }
81
}
82