Passed
Push — Showing-Posts ( edf5ba...29f02b )
by Stone
01:47
created

Category::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 0
dl 0
loc 23
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Ajax;
4
5
use App\Models\CategoryModel;
6
use Core\AjaxController;
7
8
class Category extends AjaxController
9
{
10
11
12
    public function new()
13
    {
14
        //security checks
15
        $this->onlyAdmin();
16
        if (!$this->container->getRequest()->isPost()) {
17
            throw new JsonException('Call is not post');
0 ignored issues
show
Bug introduced by
The type App\Controllers\Ajax\JsonException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
        }
19
20
        //prepating our return results
21
        $result = array();
22
        $categoryUpdateJson = ($this->request->getData('category-new'));
23
        $categoryUpdate = json_decode($categoryUpdateJson);
24
25
        //Converting our array of objects to simple array
26
        $send = array();
27
        foreach ($categoryUpdate as $item) {
28
            $send[$item->name] = $item->value;
29
        }
30
31
        $categoryModel = new CategoryModel($this->container);
32
        $result['success'] = $categoryModel->new($send["category_name"], $send["categories_slug"]);
33
        echo json_encode($result);
34
    }
35
36
    /**
37
     * Update the category via ajax
38
     * @throws \Core\JsonException
39
     */
40
    public function update()
41
    {
42
        //security checks
43
        $this->onlyAdmin();
44
        if (!$this->container->getRequest()->isPost()) {
45
            throw new JsonException('Call is not post');
46
        }
47
48
        //prepating our return results
49
        $result = array();
50
        $categoryUpdateJson = ($this->request->getData('category-update'));
51
        $categoryUpdate = json_decode($categoryUpdateJson);
52
53
        //Converting our array of objects to simple array
54
        $send = array();
55
        foreach ($categoryUpdate as $item) {
56
            $send[$item->name] = $item->value;
57
        }
58
59
        $categoryModel = new CategoryModel($this->container);
60
        $result['success'] = $categoryModel->update($send["idcategories"], $send["category_name"],
61
            $send["categories_slug"]);
62
        echo json_encode($result);
63
    }
64
65
    /**
66
     * @throws \Core\JsonException
67
     */
68
    public function delete()
69
    {
70
        //security checks
71
        $this->onlyAdmin();
72
        if (!$this->container->getRequest()->isPost()) {
73
            throw new JsonException('Call is not post');
74
        }
75
76
        //prepating our return results
77
        $result = array();
78
        $categoryDeleteJson = ($this->request->getData('category-delete'));
79
        $categoryDelete = json_decode($categoryDeleteJson);
80
81
        //Converting our array of objects to simple array
82
        $send = array();
83
        foreach ($categoryDelete as $item) {
84
            $send[$item->name] = $item->value;
85
        }
86
87
        $categoryModel = new CategoryModel($this->container);
88
        $result['success'] = $categoryModel->delete($send["idcategories"]);
89
        echo json_encode($result);
90
    }
91
}