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