Passed
Push — Showing-Posts ( ca7424...b80001 )
by Stone
02:25
created

Post::modifyPost()   C

Complexity

Conditions 11
Paths 192

Size

Total Lines 76
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 48
nc 192
nop 0
dl 0
loc 76
rs 6.55
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Controllers\Admin;
4
5
use App\Models\CategoryModel;
6
use App\Models\PostModel;
7
use App\Models\SlugModel;
8
use App\Models\TagsModel;
9
use Core\AdminController;
10
11
class Post extends AdminController
12
{
13
14
    /**
15
     * page for new post
16
     */
17
    public function new()
18
    {
19
        $this->onlyAdmin();
20
        $categoryModel = new CategoryModel($this->container);
21
        $tagModel = new TagsModel($this->container);
22
        $this->data['categories'] = $categoryModel->getCategories();
23
        $this->data['tags'] = $tagModel->getTags();
24
        $this->renderView('Admin/NewPost');
25
    }
26
27
    /**
28
     * Lists all the posts
29
     */
30
    public function list()
31
    {
32
        $this->onlyAdmin();
33
    }
34
35
    /**
36
     * Shows the post to modify and update
37
     * @throws \ReflectionException
38
     * @throws \Twig_Error_Loader
39
     * @throws \Twig_Error_Runtime
40
     * @throws \Twig_Error_Syntax
41
     * @throws \ErrorException
42
     */
43
    public function modify(string $slug): void
44
    {
45
        $this->onlyAdmin();
46
47
        $categoryModel = new CategoryModel($this->container);
48
        $tagModel = new TagsModel($this->container);
49
        $postModel = new PostModel($this->container);
50
        $slugModel = new SlugModel($this->container);
51
52
        $postId = $slugModel->getIdFromSlug($slug, "posts", "posts_slug", "idposts");
53
54
        $this->data['post'] = $postModel->getSinglePost($postId);
55
        $this->data['postTags'] = $tagModel->getTagsOnPost($postId);
56
        $this->data['categories'] = $categoryModel->getCategories();
57
        $this->data['tags'] = $tagModel->getTags();
58
        $this->renderView('Admin/ModifyPost');
59
    }
60
61
    /**
62
     * Create a new post
63
     * @throws \ErrorException
64
     */
65
    public function createNewPost()
66
    {
67
        //Security checks
68
        $this->onlyAdmin();
69
        if (!$this->request->isPost()) {
70
            $this->alertBox->setAlert('Only post messages allowed', 'error');
71
            $this->response->redirect('admin');
72
        }
73
74
        $posts = $this->container->getRequest()->getDataFull();
75
        $userSessionid = $this->container->getSession()->get("user_id");
76
77
78
        $title = trim($posts["newPostTitle"]);
79
        $postImage = $posts["newPostImage"]; //TODO Sanatize the input ? Or will PDO be enough ?
80
        $postSlug = trim($posts["newPostSlug"]); //TODO Check if unique
81
        $article = $posts["newPostTextArea"];
82
        $idCategory = $posts["categorySelector"];
83
        $published = $posts["isPublished"];
84
        $onFrontpage = $posts["isOnFrontPage"];
85
        $idUser = $userSessionid;
86
87
        $slugModel = new SlugModel($this->container);
88
        $tagModel = new TagsModel($this->container);
89
        $postModel = new PostModel($this->container);
90
91
        //security and error checks
92
        $error = false;
93
        if ($title == "") {
94
            $error = true;
95
            $this->alertBox->setAlert("empty title not allowed", "error");
96
        }
97
        if ($postSlug == "") {
98
            $error = true;
99
            $this->alertBox->setAlert("empty slug not allowed", "error");
100
        }
101
        if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) {
102
            $error = true;
103
            $this->alertBox->setAlert("Slug not unique", "error");
104
        }
105
106
        if ($error) {
107
            $this->container->getResponse()->redirect("admin/post/new");
108
        }
109
110
        $postId = $postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage,
0 ignored issues
show
Bug introduced by
It seems like $idUser can also be of type null; however, parameter $idUser of App\Models\PostModel::newPost() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        $postId = $postModel->newPost($title, $postImage, $idCategory, $article, /** @scrutinizer ignore-type */ $idUser, $published, $onFrontpage,
Loading history...
111
            $postSlug);
112
113
        if (isset($posts["tags"])) {
114
            foreach ($posts["tags"] as $tag) {
115
                if (isset($tag["id"])) {
116
                    $tagModel->addTagToPost($postId, $tag["id"]);
117
                    continue;
118
                }
119
                $tagModel->addNewTagToPost($postId, $tag["name"]);
120
            }
121
        }
122
123
        if ($postId != null) {
124
            $this->alertBox->setAlert("Post " . $title . " Created");
125
            $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug);
126
        }
127
        $this->alertBox->setAlert("Error creating " . $title, "error");
128
        $this->container->getResponse()->redirect("admin/post/new");
129
130
    }
131
132
    /**
133
     * update a post
134
     * @throws \Exception
135
     */
136
    public function modifyPost()
137
    {
138
        //Security checks
139
        $this->onlyAdmin();
140
        if (!$this->request->isPost()) {
141
            $this->alertBox->setAlert('Only post messages allowed', 'error');
142
            $this->response->redirect('admin');
143
        }
144
145
        $posts = $this->container->getRequest()->getDataFull();
146
        $userSessionid = $this->container->getSession()->get("user_id");
147
148
        $postId = $posts["postId"];
149
        $title = trim($posts["postTitle"]);
150
        $postImage = $posts["postImage"]; //TODO Sanatize the input ? Or will PDO be enough ?
151
        $postSlug = trim($posts["postSlug"]);
152
        $article = $posts["postTextArea"];
153
        $idCategory = $posts["categorySelector"];
154
        $published = $posts["isPublished"];
155
        $onFrontpage = $posts["isOnFrontPage"];
156
        $idUser = $userSessionid;
157
158
159
        $slugModel = new SlugModel($this->container);
160
        $tagModel = new TagsModel($this->container);
161
        $postModel = new PostModel($this->container);
162
163
        //security and error checks
164
        $originalPostSlug = $slugModel->getSlugFromId($postId, "posts", "idposts",
165
            "posts_slug");
166
        $error = false;
167
        if ($title == "") {
168
            $error = true;
169
            $this->alertBox->setAlert("empty title not allowed", "error");
170
        }
171
172
        if ($postSlug == "") {
173
            $error = true;
174
            $this->alertBox->setAlert("empty slug not allowed", "error");
175
        }
176
177
        if ($postSlug != $originalPostSlug) //if the slug has been updated
178
        {
179
            if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) {
180
                $error = true;
181
                $originalPostSlug = $slugModel->getSlugFromId($postId, "posts", "idposts", "posts_slug");
182
                $this->alertBox->setAlert("Slug not unique", "error");
183
            }
184
        }
185
        if ($error) {
186
            $this->container->getResponse()->redirect("admin/post/modify/$originalPostSlug");
187
        }
188
189
        $postUpdate = $postModel->modifyPost($postId, $title, $postImage, $idCategory, $article, $idUser, $published,
0 ignored issues
show
Bug introduced by
It seems like $idUser can also be of type null; however, parameter $idUser of App\Models\PostModel::modifyPost() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

189
        $postUpdate = $postModel->modifyPost($postId, $title, $postImage, $idCategory, $article, /** @scrutinizer ignore-type */ $idUser, $published,
Loading history...
190
            $onFrontpage, $postSlug);
191
192
        // Tags
193
        //remove all tags
194
        $tagModel->removeTagsOnPost($postId);
195
        //set new tags
196
        if (isset($posts["tags"])) {
197
            foreach ($posts["tags"] as $tag) {
198
                if (isset($tag["id"])) {
199
                    $tagModel->addTagToPost($postId, $tag["id"]);
200
                    continue;
201
                }
202
                $tagModel->addNewTagToPost($postId, $tag["name"]);
203
            }
204
        }
205
206
        if ($postUpdate) {
207
            $this->alertBox->setAlert("Post " . $title . " Updated");
208
            $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug);
209
        }
210
        $this->alertBox->setAlert("Error updating " . $title, "error");
211
        $this->container->getResponse()->redirect("admin/post/modify/" . $originalPostSlug);
212
213
    }
214
}