Passed
Pull Request — master (#61)
by Stone
08:36 queued 05:08
created

PostVerification::isSlugUnique()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 0
loc 27
rs 9.7666
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;
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 = (int)$this->request->getData("postId");
36
37
        $data = false;
38
        if (!$this->slug->isSlugValid($postSlug)) {
39
            echo json_encode($data);
40
            die();
41
        }
42
43
        $postModel = new PostModel($this->container);
44
45
        $data = $postModel->isPostSlugUnique(/** @scrutinizer ignore-type */$postSlug); //we have checked that slug is valid so no type error
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
}