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 |
||
155 | public function modifyPost() |
||
156 | { |
||
157 | //Security checks |
||
158 | $this->onlyAdmin(); |
||
159 | if (!$this->request->isPost()) { |
||
160 | $this->alertBox->setAlert('Only post messages allowed', 'error'); |
||
161 | $this->response->redirect('admin'); |
||
162 | } |
||
163 | |||
164 | $posts = $this->container->getRequest()->getDataFull(); |
||
165 | |||
166 | $postId = $posts["postId"]; |
||
167 | $title = trim($posts["postTitle"]); |
||
168 | $postImage = $posts["postImage"]; |
||
169 | $postSlug = trim($posts["postSlug"]); |
||
170 | $article = $posts["postTextArea"]; |
||
171 | $idCategory = $posts["categorySelector"]; |
||
172 | $published = $posts["isPublished"]; |
||
173 | $onFrontpage = $posts["isOnFrontPage"]; |
||
174 | |||
175 | $slugModel = new SlugModel($this->container); |
||
176 | $tagModel = new TagModel($this->container); |
||
177 | $postModel = new PostModel($this->container); |
||
178 | |||
179 | //security and error checks |
||
180 | $originalPostSlug = $slugModel->getSlugFromId($postId, "posts", "idposts", |
||
181 | "posts_slug"); |
||
182 | $error = false; |
||
183 | if ($title == "") { |
||
184 | $error = true; |
||
185 | $this->alertBox->setAlert("empty title not allowed", "error"); |
||
186 | } |
||
187 | |||
188 | if ($postSlug == "") { |
||
189 | $error = true; |
||
190 | $this->alertBox->setAlert("empty slug not allowed", "error"); |
||
191 | } |
||
192 | |||
193 | if ($postSlug != $originalPostSlug) //if the slug has been updated |
||
194 | { |
||
195 | if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) { |
||
196 | $error = true; |
||
197 | $originalPostSlug = $slugModel->getSlugFromId($postId, "posts", "idposts", "posts_slug"); |
||
198 | $this->alertBox->setAlert("Slug not unique", "error"); |
||
199 | } |
||
200 | } |
||
201 | if ($error) { |
||
202 | $this->container->getResponse()->redirect("admin/post/modify/$originalPostSlug"); |
||
203 | } |
||
204 | |||
205 | //Update the post |
||
206 | $postUpdate = $postModel->modifyPost($postId, $title, $postImage, $idCategory, $article, $published, |
||
207 | $onFrontpage, $postSlug); |
||
208 | |||
209 | // Tags |
||
210 | //remove all tags |
||
211 | $tagModel->removeTagsOnPost($postId); |
||
212 | //set new tags |
||
213 | if (isset($posts["tags"])) { |
||
214 | foreach ($posts["tags"] as $tag) { |
||
215 | if (isset($tag["id"])) { |
||
216 | $tagModel->addTagToPost($postId, $tag["id"]); |
||
217 | continue; |
||
218 | } |
||
219 | $tagModel->addNewTagToPost($postId, $tag["name"]); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | //checking result and redirecting |
||
224 | if ($postUpdate) { |
||
225 | $this->alertBox->setAlert("Post " . $title . " Updated"); |
||
226 | $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug); |
||
227 | } |
||
228 | $this->alertBox->setAlert("Error updating " . $title, "error"); |
||
229 | $this->container->getResponse()->redirect("admin/post/modify/" . $originalPostSlug); |
||
230 | } |
||
231 | } |