UpdateForm   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 78
ccs 0
cts 24
cp 0
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A callbackSubmit() 0 5 1
A __construct() 0 25 1
A callbackSuccess() 0 3 1
A getItemDetails() 0 6 1
1
<?php
2
3
namespace Lyco\Comment\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Lyco\Comment\Comment;
8
9
/**
10
 * Form to update an item.
11
 */
12
class UpdateForm extends FormModel
13
{
14
    public $comment;
15
    public $id;
16
    /**
17
     * Constructor injects with DI container and the id to update.
18
     *
19
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Lyco\Comment\HTMLForm\Ps...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
20
     * @param integer             $id to update
21
     */
22
    public function __construct(ContainerInterface $di, $id)
23
    {
24
        parent::__construct($di);
25
        $this->id = $id;
26
        $this->comment = $this->getItemDetails($id);
27
        $this->form->create(
28
            [
29
                "id" => __CLASS__,
30
                "legend" => "Update comment",
31
            ],
32
            [
33
                "text" => [
34
                    "type" => "text",
35
                    "validation" => ["not_empty"],
36
                    "value" => $this->comment->text
37
                ],
38
39
                "submit" => [
40
                    "type" => "submit",
41
                    "value" => "Save",
42
                    "callback" => [$this, "callbackSubmit"]
43
                ],
44
45
                "reset" => [
46
                    "type"      => "reset",
47
                ],
48
            ]
49
        );
50
    }
51
52
53
54
    /**
55
     * Get details on item to load form with.
56
     *
57
     * @param integer $id get details on item with id.
58
     *
59
     * @return Comment
60
     */
61
    public function getItemDetails($id) : object
62
    {
63
        $comment = new Comment();
64
        $comment->setDb($this->di->get("dbqb"));
65
        $comment->find("commentId", $id);
66
        return $comment;
67
    }
68
69
    /**
70
     * Callback for submit-button which should return true if it could
71
     * carry out its work and false if something failed.
72
     *
73
     * @return bool true if okey, false if something went wrong.
74
     */
75
    public function callbackSubmit() : bool
76
    {
77
        $this->comment->text = $this->form->value("text");
78
        $this->comment->save();
79
        return true;
80
    }
81
82
    /**
83
     * Callback what to do if the form was successfully submitted, this
84
     * happen when the submit callback method returns true. This method
85
     * can/should be implemented by the subclass for a different behaviour.
86
     */
87
    public function callbackSuccess()
88
    {
89
        $this->di->get("response")->redirect("post/view/" . $this->comment->postId)->send();
90
    }
91
}
92