Passed
Push — Accessing-and-setting-the-site... ( 504336...41a091 )
by Stone
02:26
created

Update::updateConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 0
dl 0
loc 27
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 Update extends AjaxController
11
{
12
    use StringFunctions;
13
14
    public function updateConfig()
15
    {
16
        $this->onlyAdmin();
17
        if (!$this->container->getRequest()->isPost()) {
18
            throw new JsonException('Call is not post');
19
        }
20
        //prepating our return results
21
        $result = array();
22
        $result['successId'] = [];
23
        $result['errorId'] = [];
24
        $result['success'] = true;
25
        $configUpdateJson = $this->container->getRequest()->getData('config-update');
26
        $configUpdate = json_decode($configUpdateJson);
27
28
        $configModel = new ConfigModel($this->container);
29
30
        foreach ($configUpdate as $update) {
31
32
            if (!$configModel->updateConfig($update->dbId, $update->value)) {
33
                $result['success'] = false;
34
                $result['errorId'][] = $update->id;
35
            } else {
36
                $result['successId'][] = $update->id;
37
            }
38
        }
39
40
        echo json_encode($result);
41
    }
42
}