| Conditions | 11 |
| Paths | 192 |
| Total Lines | 76 |
| Code Lines | 48 |
| 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 |
||
| 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, |
||
| 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 | |||
| 214 | } |