Passed
Push — master ( 76fde7...d6d75c )
by Magnus
01:55
created

CreatePostForm::createCategory()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 35
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 28
nc 32
nop 2
crap 56
1
<?php
2
3
namespace Radchasay\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\Comment\Post;
8
use \Radchasay\User\User;
9
use \Radchasay\Comment\PostCategory;
10
use \Radchasay\Comment\Post2Cat;
11
12
/**
13
 * Example of FormModel implementation.
14
 */
15
class CreatePostForm extends FormModel
16
{
17
    /**
18
     * Constructor injects with DI container.
19
     *
20
     * @param Anax\DI\DIInterface $di a service container
21
     */
22 2 View Code Duplication
    public function __construct(DIInterface $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...
23
    {
24 2
        parent::__construct($di);
25
26 2
        $this->form->create(
27
            [
28 2
                "id"     => __CLASS__,
29 2
                "legend" => "Create Post",
30 2
            ],
31
            [
32
                "title" => [
33 2
                    "type" => "text",
34 2
                ],
35
36
                "text" => [
37 2
                    "type" => "text",
38 2
                ],
39
40
                "tags" => [
41 2
                    "type"        => "text",
42 2
                ],
43
44
                "submit" => [
45 2
                    "type"     => "submit",
46 2
                    "value"    => "Create Post",
47 2
                    "callback" => [$this, "callbackSubmit"],
48 2
                ],
49
            ]
50 2
        );
51 2
    }
52
53
54
    /**
55
     * Callback for submit-button which should return true if it could
56
     * carry out its work and false if something failed.
57
     *
58
     * @return boolean true if okey, false if something went wrong.
59
     */
60 View Code Duplication
    public function callbackSubmit()
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...
61
    {
62
        // Get values from the submitted form
63
        //
64
        $post = new Post();
65
        $post->setDB($this->di->get("db"));
66
        $post->posttitle = htmlentities($this->form->value("title"));
67
        $data = $this->form->value("text");
68
        $text = $this->di->get("textfilter")->doFilter($data, ["shortcode", "markdown", "clickable", "bbcode"]);
69
70
        $post->posttext = $text;
71
        $post->postname = htmlentities($this->di->get("session")->get("email"));
72
73
        $tags = htmlspecialchars($this->form->value("tags"));
74
75
        $user = new User();
76
        $user->setDb($this->di->get("db"));
77
        $user->getInformation($post->postname);
78
        $user->points += 2;
0 ignored issues
show
Bug introduced by
The property points does not seem to exist in Radchasay\User\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
79
80
        $user->save();
81
        $post->save();
82
83
84
        $this->createCategory($tags, $post->id);
85
86
        $url = $this->di->get("url")->create("comment/viewAllPosts");
87
        $this->di->get("response")->redirect($url);
88
        return true;
89
    }
90
91
92
    public function createCategory($tags, $postId)
93
    {
94
        $postcat = new PostCategory();
95
        $postcat->setDb($this->di->get("db"));
96
        $allCats = $postcat->findall();
97
98
        $existingCats = [];
99
100
        foreach ($allCats as $ac) {
101
            array_push($existingCats, $ac->category);
102
        }
103
104
        $tags = array_map('trim', explode(',', $tags));
105
106
        $tags = array_map("strtoupper", $tags);
107
108
        $uniqueTags = [];
109
110
        foreach ($tags as $t) {
111
            if (!in_array($t, $existingCats)) {
112
                if (!in_array($t, $uniqueTags)) {
113
                    array_push($uniqueTags, $t);
114
                }
115
            }
116
        }
117
118
        foreach ($uniqueTags as $ut) {
119
            $pc = new PostCategory();
120
            $pc->setDb($this->di->get("db"));
121
            $pc->category = $ut;
122
123
            $pc->save();
124
        }
125
        //
126
        foreach ($tags as $t) {
127
            $pc = new PostCategory();
128
            $pc->setDb($this->di->get("db"));
129
130
            $id = $pc->getId($t);
131
132
            $p2c = new Post2Cat();
133
            $p2c->setDb($this->di->get("db"));
134
            $p2c->cat_id = $id;
0 ignored issues
show
Bug introduced by
The property cat_id does not seem to exist. Did you mean catid?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
135
            $p2c->post_id = $postId;
0 ignored issues
show
Bug introduced by
The property post_id does not seem to exist. Did you mean postid?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
136
137
            $p2c->save();
138
        }
139
    }
140
}
141