| Conditions | 12 |
| Paths | 130 |
| Total Lines | 70 |
| Code Lines | 44 |
| 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 |
||
| 78 | public function createNewPost() |
||
| 79 | { |
||
| 80 | //Security checks |
||
| 81 | $this->onlyAdmin(); |
||
| 82 | if (!$this->request->isPost()) { |
||
| 83 | $this->alertBox->setAlert('Only post messages allowed', 'error'); |
||
| 84 | $this->response->redirect('admin'); |
||
| 85 | } |
||
| 86 | |||
| 87 | $posts = $this->container->getRequest()->getDataFull(); |
||
| 88 | $userSessionid = $this->container->getSession()->get("user_id"); |
||
| 89 | |||
| 90 | |||
| 91 | $title = trim($posts["postTitle"]); |
||
| 92 | $postImage = $posts["postImage"]; //TODO Sanatize the input ? Or will PDO be enough ? |
||
| 93 | $postSlug = trim($posts["postSlug"]); |
||
| 94 | $article = $posts["postTextArea"]; |
||
| 95 | $idCategory = $posts["categorySelector"]; |
||
| 96 | $published = $posts["isPublished"]; |
||
| 97 | $onFrontpage = $posts["isOnFrontPage"]; |
||
| 98 | $idUser = $userSessionid; |
||
| 99 | |||
| 100 | if(!is_int($idUser) || $idUser === null) |
||
| 101 | { |
||
| 102 | throw new \Error("Invalid userID"); |
||
| 103 | } |
||
| 104 | |||
| 105 | $slugModel = new SlugModel($this->container); |
||
| 106 | $tagModel = new TagModel($this->container); |
||
| 107 | $postModel = new PostModel($this->container); |
||
| 108 | |||
| 109 | //security and error checks |
||
| 110 | $error = false; |
||
| 111 | if ($title == "") { |
||
| 112 | $error = true; |
||
| 113 | $this->alertBox->setAlert("empty title not allowed", "error"); |
||
| 114 | } |
||
| 115 | if ($postSlug == "") { |
||
| 116 | $error = true; |
||
| 117 | $this->alertBox->setAlert("empty slug not allowed", "error"); |
||
| 118 | } |
||
| 119 | if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) { |
||
| 120 | $error = true; |
||
| 121 | $this->alertBox->setAlert("Slug not unique", "error"); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($error) { |
||
| 125 | $this->container->getResponse()->redirect("admin/post/new"); |
||
| 126 | } |
||
| 127 | |||
| 128 | $postId = $postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage, |
||
| 129 | $postSlug); |
||
| 130 | |||
| 131 | if (isset($posts["tags"])) { |
||
| 132 | foreach ($posts["tags"] as $tag) { |
||
| 133 | if (isset($tag["id"])) { |
||
| 134 | $tagModel->addTagToPost($postId, $tag["id"]); |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | $tagModel->addNewTagToPost($postId, $tag["name"]); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | //checking result and redirecting |
||
| 142 | if ($postId != null) { |
||
| 143 | $this->alertBox->setAlert("Post " . $title . " Created"); |
||
| 144 | $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug); |
||
| 145 | } |
||
| 146 | $this->alertBox->setAlert("Error creating " . $title, "error"); |
||
| 147 | $this->container->getResponse()->redirect("admin/post/new"); |
||
| 148 | |||
| 231 | } |