Passed
Pull Request — master (#57)
by Stone
03:27
created

postModification::modifyPublished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.9666
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\Container;
8
9
class postModification extends AjaxController
10
{
11
12
13
    private $postModule;
14
15
    public function __construct(Container $container)
16
    {
17
        parent::__construct($container);
18
        $this->postModule = new PostModel($this->container);
19
    }
20
21
    /**
22
     * Ajax call to update the published state of a post
23
     * @throws \Core\JsonException
24
     */
25
    public function modifyPublished()
26
    {
27
        $this->onlyAdmin();
28
        $this->onlyPost();
29
        $state = (bool)($this->request->getData("state") === 'true');
30
        $postId = (int)$this->request->getData("postId");
31
32
        $result = array();
33
        $result["success"] = $this->postModule->setPublished(!$state, $postId);
34
        $result["state"] = !$state;
35
        $result["postId"] = $postId;
36
        echo json_encode($result);
37
    }
38
39
    /**
40
     * Ajax call to update the on front page state of a post
41
     * @throws \Core\JsonException
42
     */
43
    public function modifyOnFrontPage()
44
    {
45
        $this->onlyAdmin();
46
        $this->onlyPost();
47
        $state = (bool)($this->request->getData("state") === 'true');
48
        $postId = (int)$this->request->getData("postId");
49
50
        $result = array();
51
        $result["success"] = $this->postModule->setOnFrontPage(!$state, $postId);
52
        $result["state"] = !$state;
53
        $result["postId"] = $postId;
54
        echo json_encode($result);
55
    }
56
}