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