PostVerification   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isSlugUnique() 0 25 4
A __construct() 0 5 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;
13
14
15
    protected $slug;
16
17
    private $postModel;
18
19
    public function __construct(Container $container)
20
    {
21
        $this->loadModules[] = 'Slug';
22
        parent::__construct($container);
23
        $this->postModel = new PostModel($container);
24
    }
25
26
    /**
27
     * checks if the slug is unique
28
     * @return bool is unique
29
     * @throws \Core\JsonException
30
     * @throws \Exception
31
     */
32
    public function isSlugUnique()
33
    {
34
        $this->onlyAdmin();
35
        $this->onlyPost();
36
37
        $postSlug = $this->request->getData("postSlug");
38
        $postId = (int)$this->request->getData("postId");
39
40
        $data = false;
41
        if (!$this->slug->isSlugValid($postSlug)) {
42
            echo json_encode($data);
43
            die();
44
        }
45
46
        $data = $this->postModel->isPostSlugUnique(/** @scrutinizer ignore-type */$postSlug); //we have checked that slug is valid so no type error
47
48
        if ($data === false) //slug is not unique, but could be from the same post
49
        {
50
            $slugOfId = $this->postModel->getPostSlugFromId($postId);
51
            if ($slugOfId === $postSlug) {
52
                //it's the same post, return true
53
                $data = true;
54
            }
55
        }
56
        echo json_encode($data);
57
    }
58
59
}