Passed
Push — Showing-Posts ( a72031...c030ab )
by Stone
02:07
created

postModification   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A modifyOnFrontPage() 0 13 2
A modifyPublished() 0 13 2
1
<?php
2
3
namespace App\Controllers\Ajax;
4
5
use App\Models\PostModel;
6
use Core\AjaxController;
7
use Core\Container;
8
9
class postModification extends AjaxController{
10
11
12
    private $postModule;
13
14
    public function __construct(Container $container)
15
    {
16
        parent::__construct($container);
17
        $this->postModule = new PostModel($this->container);
18
    }
19
20
    public function modifyPublished()
21
    {
22
        $this->onlyAdmin();
23
        if (!$this->container->getRequest()->isPost()) {
24
            throw new \Core\JsonException('Call is not post');
25
        }
26
        $state = (bool)($this->request->getData("state") === 'true');
27
        $postId = (int)$this->request->getData("postId");
28
29
        $result["success"] = $this->postModule->setPublished(!$state, $postId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
30
        $result["state"] = !$state;
31
        $result["postId"] = $postId;
32
        echo json_encode($result);
33
    }
34
35
    public function modifyOnFrontPage()
36
    {
37
        $this->onlyAdmin();
38
        if (!$this->container->getRequest()->isPost()) {
39
            throw new \Core\JsonException('Call is not post');
40
        }
41
        $state = ($this->request->getData("state") === 'true');
42
        $postId = (int)$this->request->getData("postId");
43
44
        $result["success"] = $this->postModule->setOnFrontPage(!$state, $postId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
45
        $result["state"] = !$state;
46
        $result["postId"] = $postId;
47
        echo json_encode($result);
48
    }
49
}