Conditions | 11 |
Paths | 192 |
Total Lines | 75 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |