Passed
Pull Request — dev (#47)
by Stone
04:17 queued 01:57
created

Update   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A updateConfig() 0 28 4
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 Update extends AjaxController
11
{
12
    use StringFunctions;
13
14
    /**
15
     * Update the site configuration via Ajax post
16
     * @throws JsonException
17
     */
18
    public function updateConfig()
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
            if (!$configModel->updateConfig($update->dbId, $update->value)) {
38
                $result['success'] = false;
39
                $result['errorId'][] = $update->id;
40
            } else {
41
                $result['successId'][] = $update->id;
42
            }
43
        }
44
45
        echo json_encode($result);
46
    }
47
}