Passed
Push — Auth ( 52ac6b...b5e900 )
by Stone
02:27
created

PostVerification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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);
46
47
        if ($data === false) //slug is not unique, but could be from the same post
48
        {
49
            $slugOfId = $postModel->getPostSlugFromId($postId);
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
}