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

Config::update()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 0
dl 0
loc 29
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Ajax;
4
5
use App\Models\ConfigModel;
6
use Core\AjaxController;
7
use Core\JsonException;
8
use Core\Traits\StringFunctions;
9
10
class Config extends AjaxController
11
{
12
    use StringFunctions;
13
14
    /**
15
     * Update the site configuration via Ajax post
16
     * @throws JsonException
17
     */
18
    public function update()
19
    {
20
        //security checks
21
        $this->onlyAdmin();
22
        if (!$this->container->getRequest()->isPost()) {
23
            throw new JsonException('Call is not post');
24
        }
25
        //prepating our return results
26
        $result = array();
27
        $result['successId'] = [];
28
        $result['errorId'] = [];
29
        $result['success'] = true;
30
        $configUpdateJson = $this->container->getRequest()->getData('config-update');
31
        $configUpdate = json_decode($configUpdateJson);
32
33
        $configModel = new ConfigModel($this->container);
34
35
        foreach ($configUpdate as $update) {
36
37
38
            if (!$configModel->updateConfig($update->name, $update->value)) {
39
                $result['success'] = false;
40
                $result['errorId'][] = $update->name;
41
            } else {
42
                $result['successId'][] = $update->name;
43
            }
44
        }
45
46
        echo json_encode($result);
47
    }
48
}