| Conditions | 10 |
| Paths | 130 |
| Total Lines | 61 |
| Code Lines | 37 |
| 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 |
||
| 97 | public function createNewPost() |
||
| 98 | { |
||
| 99 | //Security checks |
||
| 100 | $this->onlyAdmin(); |
||
| 101 | if (!$this->request->isPost()) { |
||
| 102 | $this->alertBox->setAlert('Only post messages allowed', 'error'); |
||
| 103 | $this->response->redirect('admin'); |
||
| 104 | } |
||
| 105 | |||
| 106 | $posts = $this->container->getRequest()->getDataFull(); |
||
| 107 | $userSessionId = $this->container->getSession()->get("user_id"); |
||
| 108 | |||
| 109 | |||
| 110 | $title = trim($posts["postTitle"]); |
||
| 111 | $postImage = $posts["postImage"]; |
||
| 112 | $postSlug = trim($posts["postSlug"]); |
||
| 113 | $article = $posts["postTextArea"]; |
||
| 114 | $idCategory = $posts["categorySelector"]; |
||
| 115 | $published = $posts["isPublished"]; |
||
| 116 | $onFrontpage = $posts["isOnFrontPage"]; |
||
| 117 | $idUser = $userSessionId; |
||
| 118 | |||
| 119 | if(!is_int($idUser) || $idUser === null) |
||
| 120 | { |
||
| 121 | throw new \Error("Invalid userID"); |
||
| 122 | } |
||
| 123 | |||
| 124 | //security and error checks |
||
| 125 | $error = false; |
||
| 126 | if ($title == "") { |
||
| 127 | $error = true; |
||
| 128 | $this->alertBox->setAlert("empty title not allowed", "error"); |
||
| 129 | } |
||
| 130 | if ($postSlug == "") { |
||
| 131 | $error = true; |
||
| 132 | $this->alertBox->setAlert("empty slug not allowed", "error"); |
||
| 133 | } |
||
| 134 | if (!$this->slugModel->isUnique($postSlug, "posts", "posts_slug")) { |
||
| 135 | $error = true; |
||
| 136 | $this->alertBox->setAlert("Slug not unique", "error"); |
||
| 137 | } |
||
| 138 | |||
| 139 | if ($error) { |
||
| 140 | $this->container->getResponse()->redirect("admin/post/new"); |
||
| 141 | } |
||
| 142 | |||
| 143 | $postId = $this->postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage, |
||
| 144 | $postSlug); |
||
| 145 | |||
| 146 | //Taking care of tags. |
||
| 147 | if (isset($posts["tags"])) { |
||
| 148 | $this->addTags($posts["tags"], $postId); |
||
| 149 | } |
||
| 150 | |||
| 151 | //checking result and redirecting |
||
| 152 | if ($postId != null) { |
||
| 153 | $this->alertBox->setAlert("Post " . $title . " Created"); |
||
| 154 | $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug); |
||
| 155 | } |
||
| 156 | $this->alertBox->setAlert("Error creating " . $title, "error"); |
||
| 157 | $this->container->getResponse()->redirect("admin/post/new"); |
||
| 158 | |||
| 231 | } |