Passed
Push — Showing-Posts ( 80fb19...3afe89 )
by Stone
01:57
created

Post::createNewPost()   C

Complexity

Conditions 12
Paths 130

Size

Total Lines 70
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 44
nc 130
nop 0
dl 0
loc 70
rs 6.7166
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
        $this->renderView("Admin/ListPost");
35
    }
36
37
    /**
38
     * Shows the post to modify and update
39
     * @throws \ReflectionException
40
     * @throws \Twig_Error_Loader
41
     * @throws \Twig_Error_Runtime
42
     * @throws \Twig_Error_Syntax
43
     * @throws \ErrorException
44
     */
45
    public function modify(string $slug): void
46
    {
47
        $this->onlyAdmin();
48
49
        $categoryModel = new CategoryModel($this->container);
50
        $tagModel = new TagsModel($this->container);
51
        $postModel = new PostModel($this->container);
52
        $slugModel = new SlugModel($this->container);
53
54
        $postId = $slugModel->getIdFromSlug($slug, "posts", "posts_slug", "idposts");
55
56
        $this->data['post'] = $postModel->getSinglePost($postId);
57
        $this->data['postTags'] = $tagModel->getTagsOnPost($postId);
58
        $this->data['categories'] = $categoryModel->getCategories();
59
        $this->data['tags'] = $tagModel->getTags();
60
        $this->renderView('Admin/ModifyPost');
61
    }
62
63
    /**
64
     * Create a new post
65
     * @throws \ErrorException
66
     */
67
    public function createNewPost()
68
    {
69
        //Security checks
70
        $this->onlyAdmin();
71
        if (!$this->request->isPost()) {
72
            $this->alertBox->setAlert('Only post messages allowed', 'error');
73
            $this->response->redirect('admin');
74
        }
75
76
        $posts = $this->container->getRequest()->getDataFull();
77
        $userSessionid = $this->container->getSession()->get("user_id");
78
79
80
        $title = trim($posts["newPostTitle"]);
81
        $postImage = $posts["newPostImage"]; //TODO Sanatize the input ? Or will PDO be enough ?
82
        $postSlug = trim($posts["newPostSlug"]);
83
        $article = $posts["newPostTextArea"];
84
        $idCategory = $posts["categorySelector"];
85
        $published = $posts["isPublished"];
86
        $onFrontpage = $posts["isOnFrontPage"];
87
        $idUser = $userSessionid;
88
89
        if(!is_int($idUser) || $idUser === null)
90
        {
91
            throw new \Error("Invalid userID");
92
        }
93
94
        $slugModel = new SlugModel($this->container);
95
        $tagModel = new TagsModel($this->container);
96
        $postModel = new PostModel($this->container);
97
98
        //security and error checks
99
        $error = false;
100
        if ($title == "") {
101
            $error = true;
102
            $this->alertBox->setAlert("empty title not allowed", "error");
103
        }
104
        if ($postSlug == "") {
105
            $error = true;
106
            $this->alertBox->setAlert("empty slug not allowed", "error");
107
        }
108
        if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) {
109
            $error = true;
110
            $this->alertBox->setAlert("Slug not unique", "error");
111
        }
112
113
        if ($error) {
114
            $this->container->getResponse()->redirect("admin/post/new");
115
        }
116
117
        $postId = $postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage,
118
            $postSlug);
119
120
        if (isset($posts["tags"])) {
121
            foreach ($posts["tags"] as $tag) {
122
                if (isset($tag["id"])) {
123
                    $tagModel->addTagToPost($postId, $tag["id"]);
124
                    continue;
125
                }
126
                $tagModel->addNewTagToPost($postId, $tag["name"]);
127
            }
128
        }
129
130
        //checking result and redirecting
131
        if ($postId != null) {
132
            $this->alertBox->setAlert("Post " . $title . " Created");
133
            $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug);
134
        }
135
        $this->alertBox->setAlert("Error creating " . $title, "error");
136
        $this->container->getResponse()->redirect("admin/post/new");
137
138
    }
139
140
    /**
141
     * update a post
142
     * @throws \Exception
143
     */
144
    public function modifyPost()
145
    {
146
        //Security checks
147
        $this->onlyAdmin();
148
        if (!$this->request->isPost()) {
149
            $this->alertBox->setAlert('Only post messages allowed', 'error');
150
            $this->response->redirect('admin');
151
        }
152
153
        $posts = $this->container->getRequest()->getDataFull();
154
155
        $postId = $posts["postId"];
156
        $title = trim($posts["postTitle"]);
157
        $postImage = $posts["postImage"];
158
        $postSlug = trim($posts["postSlug"]);
159
        $article = $posts["postTextArea"];
160
        $idCategory = $posts["categorySelector"];
161
        $published = $posts["isPublished"];
162
        $onFrontpage = $posts["isOnFrontPage"];
163
164
        $slugModel = new SlugModel($this->container);
165
        $tagModel = new TagsModel($this->container);
166
        $postModel = new PostModel($this->container);
167
168
        //security and error checks
169
        $originalPostSlug = $slugModel->getSlugFromId($postId, "posts", "idposts",
170
            "posts_slug");
171
        $error = false;
172
        if ($title == "") {
173
            $error = true;
174
            $this->alertBox->setAlert("empty title not allowed", "error");
175
        }
176
177
        if ($postSlug == "") {
178
            $error = true;
179
            $this->alertBox->setAlert("empty slug not allowed", "error");
180
        }
181
182
        if ($postSlug != $originalPostSlug) //if the slug has been updated
183
        {
184
            if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) {
185
                $error = true;
186
                $originalPostSlug = $slugModel->getSlugFromId($postId, "posts", "idposts", "posts_slug");
187
                $this->alertBox->setAlert("Slug not unique", "error");
188
            }
189
        }
190
        if ($error) {
191
            $this->container->getResponse()->redirect("admin/post/modify/$originalPostSlug");
192
        }
193
194
        //Update the post
195
        $postUpdate = $postModel->modifyPost($postId, $title, $postImage, $idCategory, $article, $published,
196
            $onFrontpage, $postSlug);
197
198
        // Tags
199
        //remove all tags
200
        $tagModel->removeTagsOnPost($postId);
201
        //set new tags
202
        if (isset($posts["tags"])) {
203
            foreach ($posts["tags"] as $tag) {
204
                if (isset($tag["id"])) {
205
                    $tagModel->addTagToPost($postId, $tag["id"]);
206
                    continue;
207
                }
208
                $tagModel->addNewTagToPost($postId, $tag["name"]);
209
            }
210
        }
211
212
        //checking result and redirecting
213
        if ($postUpdate) {
214
            $this->alertBox->setAlert("Post " . $title . " Updated");
215
            $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug);
216
        }
217
        $this->alertBox->setAlert("Error updating " . $title, "error");
218
        $this->container->getResponse()->redirect("admin/post/modify/" . $originalPostSlug);
219
    }
220
}