Passed
Pull Request — master (#57)
by Stone
03:27
created

PostVerification   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isSlugUnique() 0 27 5
A __construct() 0 4 1
1
<?php
2
3
namespace App\Controllers\Ajax;
4
5
use App\Models\PostModel;
6
use Core\AjaxController;
7
use Core\Traits\StringFunctions;
8
use Core\Container;
9
10
class PostVerification extends AjaxController
11
{
12
    use StringFunctions;
1 ignored issue
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Controllers\Ajax\PostVerification.
Loading history...
13
14
15
    protected $slug;
16
17
    public function __construct(Container $container)
18
    {
19
        $this->loadModules[] = 'Slug';
20
        parent::__construct($container);
21
    }
22
23
    /**
24
     * checks if the slug is unique
25
     * @return bool is unique
26
     * @throws \Core\JsonException
27
     * @throws \Exception
28
     */
29
    public function isSlugUnique()
30
    {
31
        $this->onlyAdmin();
32
        $this->onlyPost();
33
34
        $postSlug = $this->request->getData("postSlug");
35
        $postId = $this->request->getData("postId");
36
37
        $data = false;
38
        if (!$this->slug->isSlugValid($postSlug) || !$this->isInt($postId)) {
39
            echo json_encode($data);
40
            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...
41
        }
42
43
        $postModel = new PostModel($this->container);
44
45
        $data = $postModel->isPostSlugUnique($postSlug);
0 ignored issues
show
Bug introduced by
It seems like $postSlug can also be of type null; however, parameter $postSlug of App\Models\PostModel::isPostSlugUnique() does only seem to accept string, 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

45
        $data = $postModel->isPostSlugUnique(/** @scrutinizer ignore-type */ $postSlug);
Loading history...
46
47
        if ($data === false) //slug is not unique, but could be from the same post
48
        {
49
            $slugOfId = $postModel->getPostSlugFromId($postId);
0 ignored issues
show
Bug introduced by
It seems like $postId can also be of type null; however, parameter $postId of App\Models\PostModel::getPostSlugFromId() 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

49
            $slugOfId = $postModel->getPostSlugFromId(/** @scrutinizer ignore-type */ $postId);
Loading history...
50
            if ($slugOfId === $postSlug) {
51
                //it's the same post, return true
52
                $data = true;
53
            }
54
        }
55
        echo json_encode($data);
56
    }
57
58
}