Completed
Push — master ( 0d0ae6...7c7732 )
by Stone
19s
created

PostVerification   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A isSlugUnique() 0 29 5
1
<?php
2
3
namespace App\Controllers\Ajax;
4
5
use App\Models\PostModel;
6
use Core\AjaxController;
7
use Core\Traits\StringFunctions;
8
9
class PostVerification extends AjaxController
10
{
11
    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...
12
13
    /**
14
     * checks if the slug is unique
15
     * @return bool is unique
16
     * @throws \Core\JsonException
17
     * @throws \Exception
18
     */
19
    public function isSlugUnique()
20
    {
21
        $this->onlyAdmin();
22
        if (!$this->container->getRequest()->isPost()) {
23
            throw new \Core\JsonException('Call is not post');
24
        }
25
26
        $postSlug = $this->request->getData("postSlug");
27
        $postId = $this->request->getData("postId");
28
29
        $data = false;
30
        if (!$this->isAlphaNum($postSlug)) {
31
            echo json_encode($data);
32
            return true;
33
        }
34
35
        $postModel = new PostModel($this->container);
36
37
        $data = $postModel->isPostSlugUnique($postSlug);
38
39
        if ($data === false) //slug is not unique, but could be from the same post
40
        {
41
            $slugOfId = $postModel->getPostSlugFromId($postId);
42
            if ($slugOfId === $postSlug) {
43
                //it's the same post, return true
44
                $data = true;
45
            }
46
        }
47
        echo json_encode($data);
48
    }
49
50
}