Test Setup Failed
Push — master ( 3028d7...678984 )
by Markus
03:32
created

CreatePostForm::getItemDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Anax\Forum\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Psr\Container\ContainerInterface;
7
use Anax\Forum\Forum;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class CreatePostForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
Bug introduced by
The type Anax\Forum\HTMLForm\Psr\...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct(ContainerInterface $di)
20
    {
21
        parent::__construct($di);
22
        $user = $this->di->get("session")->get("login");
23
        $this->form->create(
24
            [
25
                "id" => __CLASS__,
26
                "legend" => "Create a forum post",
27
            ],
28
            [
29
                "title" => [
30
                    "type"        => "text",
31
                ],
32
33
                "content" => [
34
                    "type"        => "textarea",
35
                    "placeholder" => "Write the post in markdown to change how it looks.",
36
                ],
37
38
                "tag" => [
39
                    "type"        => "text",
40
                ],
41
42
                "user" => [
43
                    "type" => "text",
44
                    "value" => $user,
45
                    "readonly" => "readonly",
46
                ],
47
48
                "submit" => [
49
                    "type" => "submit",
50
                    "value" => "Skapa inlägg",
51
                    "callback" => [$this, "callbackSubmit"]
52
                ],
53
            ]
54
        );
55
    }
56
57
    /**
58
     * Get details on item to load form with.
59
     *
60
     * @param integer $id get details on item with id.
61
     *
62
     * @return User
0 ignored issues
show
Bug introduced by
The type Anax\Forum\HTMLForm\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
     */
64
    public function getItemDetails($id) : object
65
    {
66
        $user = new \Anax\User\User();
67
        $user->setDb($this->di->get("dbqb"));
68
        $user->find("id", $id);
69
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type Anax\User\User which is incompatible with the documented return type Anax\Forum\HTMLForm\User.
Loading history...
70
    }
71
72
    /**
73
     * Callback for submit-button which should return true if it could
74
     * carry out its work and false if something failed.
75
     *
76
     * @return boolean true if okey, false if something went wrong.
77
     */
78
     public function callbackSubmit()
79
    {
80
        // Get values from the submitted form
81
        $login = $this->di->get("session")->get("login");
82
        $poster = $this->getItemDetails($login);
83
        $title = $this->form->value("title");
84
        $content = $this->form->value("content");
85
        $tag = $this->form->value("tag");
86
        $user = $this->form->value("user");
87
88
        $forum = new Forum();
89
        $forum->setDb($this->di->get("dbqb"));
90
        $forum->title = $title;
91
        $forum->content = $content;
92
        $forum->tag = $tag;
93
        $forum->user = $user;
0 ignored issues
show
Bug introduced by
The property user does not seem to exist on Anax\Forum\Forum.
Loading history...
94
        $poster->username = $poster->username;
95
        $poster->activity += 1;
96
        $poster->save();
97
        $forum->save();
98
99
        $this->form->addOutput("Post was created.");
100
        return true;
101
    }
102
103
    public function callbackSuccess()
104
    {
105
        $this->di->get("response")->redirect("forum")->send();
106
    }
107
}
108