CreateForm   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 27.19 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 31
loc 114
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 31 31 1
A callbackSubmit() 0 40 4
A callbackSuccess() 0 4 1
A callbackFail() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Forum\Question\HTMLForm;
4
5
use Anax\HTMLForm\FormModel;
6
use Forum\Tag\TagQuestion;
7
use Psr\Container\ContainerInterface;
8
use Forum\Question\Question;
9
use Forum\Tag\Tag;
10
use Forum\User\User;
11
12
13
/**
14
 * Form to create an item.
15
 */
16
class CreateForm extends FormModel
17
{
18
    /**
19
     * Constructor injects with DI container.
20
     *
21
     * @param Psr\Container\ContainerInterface $di a service container
22
     */
23 View Code Duplication
    public function __construct(ContainerInterface $di)
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...
24
    {
25
        parent::__construct($di);
26
        $this->form->create(
27
            [
28
                "id" => __CLASS__,
29
                "legend" => "Skapa fråga",
30
            ],
31
            [
32
                "titel" => [
33
                    "type" => "text",
34
                    "validation" => ["not_empty"],
35
                ],
36
37
                "fråga" => [
38
                    "type" => "textarea",
39
                    "validation" => ["not_empty"],
40
                ],
41
42
                "tags" => [
43
                    "type" => "text",
44
                ],
45
46
                "submit" => [
47
                    "type" => "submit",
48
                    "value" => "Skapa fråga",
49
                    "callback" => [$this, "callbackSubmit"]
50
                ],
51
            ]
52
        );
53
    }
54
55
56
57
    /**
58
     * Callback for submit-button which should return true if it could
59
     * carry out its work and false if something failed.
60
     *
61
     * @return bool true if okey, false if something went wrong.
62
     */
63
    public function callbackSubmit() : bool
64
    {
65
        $question = new Question();
66
        $user_id = $this->di->get("session")->get("user_id");
67
        $question->setDb($this->di->get("dbqb"));
68
        $question->title  = $this->form->value("titel");
69
        $question->question = $this->form->value("fråga");
70
//        $question->tag = $tag;
71
        $question->user_id = $user_id;
72
        $question->created = date(DATE_RFC822);
73
        $question->save();
74
75
        $tags = $this->form->value("tags");
76
        // Split comma separated $tags into $tag_name_list array of strings
77
        $tagNames = explode(",", $tags);
78
        //   Put the Tag object into $tag_list, which is an array of Tag objects
79
        $tagObjectsArray = [];
80
        foreach ($tagNames as $tagName) {
81
            $tag = new Tag();
82
            $tag->setDb($this->di->get("dbqb"));
83
            $tag->find("name", $tagName);
84
            if ($tag->id == null) {
85
                $tag->name = $tagName;
86
                $tag->save();
87
            }
88
            array_push($tagObjectsArray, $tag);
89
        }
90
        // For each Tag object in $tagObjectsArray
91
        //   Create a new TagQuestion object with tag_id and question_id
92
        //   Save it to the database
93
        foreach ($tagObjectsArray as $tag) {
94
            $tagQuestion = new TagQuestion();
95
            $tagQuestion->setDb($this->di->get("dbqb"));
96
            $tagQuestion->tag_id  = $tag->id;
97
            $tagQuestion->question_id = $question->id;
98
            $tagQuestion->save();
99
        }
100
101
        return true;
102
    }
103
104
105
106
    /**
107
     * Callback what to do if the form was successfully submitted, this
108
     * happen when the submit callback method returns true. This method
109
     * can/should be implemented by the subclass for a different behaviour.
110
     */
111
    public function callbackSuccess()
112
    {
113
        $this->di->get("response")->redirect("forum/index")->send();
114
    }
115
116
117
118
     /**
119
      * Callback what to do if the form was unsuccessfully submitted, this
120
      * happen when the submit callback method returns false or if validation
121
      * fails. This method can/should be implemented by the subclass for a
122
      * different behaviour.
123
      */
124
     public function callbackFail()
125
     {
126
         print_r("No go");
127
         $this->di->get("response")->redirectSelf()->send();
128
     }
129
}
130