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

UpdateCommForm::decode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 6
nc 1
nop 1
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
 * Example of FormModel implementation.
11
 */
12
class UpdateCommForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container and the id to update.
16
     *
17
     * @param Anax\DI\DIInterface $di a service container
18
     * @param integer             $id to update
19
     */
20 2
    public function __construct(DIInterface $di, $id, $sessid)
21
    {        
22 2
        parent::__construct($di);
23 2
        $comm = $this->getCommDetails($id);
24
25 2
        $comt = $this->decode($comm->comment);
0 ignored issues
show
Documentation introduced by
$comm->comment is of type string, but the function expects a object<Anax\Comments\HTMLForm\json>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26 2
        $this->aForm($id, $sessid, $comm, $comt);
27 2
    }
28
29
30
    /**
31
    * Converts json-string back to variables
32
    *
33
    * @param json $fromjson the jsoncode
34
    * @return the extracted comment-text
35
    */
36 2
    public function decode($fromjson)
37
    {
38 2
        $textfilter = $this->di->get("textfilter");
39 2
        $toparse = json_decode($fromjson);
40 2
        $comt = $textfilter->parse($toparse->text, ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"]);
41 2
        $comt = strip_tags($comt->text);
42 2
        return $comt;
43
    }
44
45
    /**
46
     * Create the form.
47
     *
48
     */
49 2
    public function aForm($id, $sessid, $comm, $comt)
50
    {
51 2
        $this->form->create(
52 2
            ["id" => __CLASS__, "legend" => "Uppdatera ditt konto"],
53 2
            [   "sessid" => ["type"  => "hidden", "value" => $sessid],
54 2
                "id" => ["type"  => "hidden", "value" => $id],
55 2
                "parentid" => ["type"  => "hidden", "value" => $comm->parentid],
56 2
                "title" => ["type" => "text", "validation" => ["not_empty"], "value" => $comm->title],
57 2
                "comment" => ["type" => "textarea", "validation" => ["not_empty"], "value" => $comt],
58 2
                "submit" => ["type" => "submit", "value" => "Spara", "callback" => [$this, "callbackSubmit"]],
59 2
                "reset" => ["type"      => "reset"],
60
            ]
61 2
        );
62 2
    }
63
64
65
66
    /**
67
     * Get details on item to load form with.
68
     *
69
     * @param integer $id get details on item with id.
70
     *
71
     * @return Comm
72
     */
73 2 View Code Duplication
    public 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...
74
    {
75 2
        $comm = new Comm();
76 2
        $comm->setDb($this->di->get("db"));
77 2
        $comm->find("id", $id);
78 2
        return $comm;
79
    }
80
81
82
83
    /**
84
     * Callback for submit-button which should return true if it could
85
     * carry out its work and false if something failed.
86
     *
87
     * @return boolean true if okey, false if something went wrong.
88
     */
89
    public function callbackSubmit()
90
    {
91
        $now = date("Y-m-d H:i:s");
92
93
        $textfilter = $this->di->get("textfilter");
94
95
        $userController = $this->di->get("userController");
96
        $userdetails = $userController->getOne($this->form->value("sessid"));
97
98
        $parses = ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"];
99
        $comment = $textfilter->parse($this->form->value("comment"), $parses);
100
        $comment->frontmatter['title'] = $this->form->value("title");
101
        $comment = json_encode($comment);
102
103
        $comm = new Comm();
104
        $comm->setDb($this->di->get("db"));
105
        $comm->find("id", $this->form->value("id"));
106
        $comm->updated = $now;
107
        $comm->title = $this->form->value("title");
108
        $comm->userid = $this->form->value("sessid");
109
        $comm->comment = $comment;
110
        $comm->email = $userdetails["email"];
111
        $comm->save();
112
113
        $parentid = (int)$this->form->value("parentid");
114
115
        $back = $parentid > 0 ? "/view-one/" . $parentid : "/view-one/" . $this->form->value("id");
116
117
        $pagerender = $this->di->get("pageRender");
118
        $pagerender->redirect("comm" . $back);
119
120
        return true;
121
    }
122
}
123