Passed
Push — Showing-Posts ( 5aec6a...cc8e3b )
by Stone
02:41
created

Post::createNewPost()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 53
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 30
nc 6
nop 0
dl 0
loc 53
rs 8.8177
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace App\Controllers\Admin;
4
5
use App\Models\CategoryModel;
6
use App\Models\PostModel;
7
use App\Models\SlugModel;
8
use App\Models\TagsModel;
9
use Core\AdminController;
10
11
class Post extends AdminController
12
{
13
14
    /**
15
     * page for new post
16
     */
17
    public function new()
18
    {
19
        $this->onlyAdmin();
20
        //TODO have we receved a $_POST, if yes then probably an error on the create new post
21
        $categoryModel = new CategoryModel($this->container);
22
        $tagModel = new TagsModel($this->container);
23
        $this->data['categories'] = $categoryModel->getCategories();
24
        $this->data['tags'] = $tagModel->getTags();
25
        $this->renderView('Admin/NewPost');
26
    }
27
28
    /**
29
     * Lists all the posts
30
     */
31
    public function list()
32
    {
33
        $this->onlyAdmin();
34
35
    }
36
37
    /**
38
     * Shows the post to modify and update
39
     * @param $idPost
40
     */
41
    public function modify($idPost)
0 ignored issues
show
Unused Code introduced by
The parameter $idPost is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    public function modify(/** @scrutinizer ignore-unused */ $idPost)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $this->onlyAdmin();
44
45
    }
46
47
    /**
48
     * Create a new post
49
     * @throws \ErrorException
50
     */
51
    public function createNewPost()
52
    {
53
        //Security checks
54
        $this->onlyAdmin();
55
        if (!$this->request->isPost()) {
56
            $this->alertBox->setAlert('Only post messages allowed', 'error');
57
            $this->response->redirect('admin');
58
        }
59
        $posts = $this->container->getRequest()->getDataFull();
60
        $userSessionid = $this->container->getSession()->get("user_id");
61
62
        //TODO
63
        //Slug, check if duplicate
64
        //Tags, check if duplicate before creating new tag
65
        //Tags, must have created the post and got the id before associating the tags
66
        //grab author from session
67
68
        $title = $posts["newPostTitle"];
69
        $postImage = $posts["newPostImage"]; //TODO Sanatize the input ? Or will PDO be enough ?
70
        $postSlug = $posts["newPostSlug"]; //TODO Check if unique
71
        $article = $posts["newPostTextArea"];
72
        $idCategory = $posts["categorySelector"];
73
        $published = $posts["isPublished"];
74
        $onFrontpage = $posts["isOnFrontPage"];
75
        $idUser = $userSessionid;
76
77
        $slugModel = new SlugModel($this->container);
78
        $tagModel = new TagsModel($this->container);
79
        $postModel = new PostModel($this->container);
80
81
        //security and error checks
82
        if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) {
83
            die("SLUG not unique");
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
84
        }
85
        
86
        $postId = $postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage, $postSlug);
0 ignored issues
show
Bug introduced by
It seems like $idUser can also be of type null; however, parameter $idUser of App\Models\PostModel::newPost() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $postId = $postModel->newPost($title, $postImage, $idCategory, $article, /** @scrutinizer ignore-type */ $idUser, $published, $onFrontpage, $postSlug);
Loading history...
87
88
        echo "<p>new post ID : " . $postId . "</p>";
89
90
        if(isset($posts["tags"])){
91
            foreach ($posts["tags"] as $tag) {
92
                if(isset($tag["id"])){
93
                    $tagModel->addTagToPost($postId, $tag["id"]);
94
                    continue;
95
                }
96
                $tagModel->addNewTagToPost($postId, $tag["name"]);
97
            }
98
        }
99
100
101
        echo "<pre>";
102
        var_dump($posts);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($posts) looks like debug code. Are you sure you do not want to remove it?
Loading history...
103
        die();
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
104
105
        //TODO send a return $_POST with all data to the new post page if errors (also need to check if new page recives post, then add the data back into the forms
106
107
        //TODO else redirect to the modify page on OK ? good usability or stay on the new page with blank ?
108
    }
109
}