Passed
Push — Accessing-and-setting-the-site... ( 7f6958...7a9423 )
by Stone
02:15
created

Update::updateConfig()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 31
nc 4
nop 0
dl 0
loc 47
rs 8.4906
c 0
b 0
f 0
1
<?php
2
namespace App\Controllers\Ajax;
3
4
use App\Models\ConfigModel;
5
use Core\AjaxController;
6
use Core\JsonException;
7
use Core\Traits\StringFunctions;
8
9
class Update extends AjaxController{
10
    use StringFunctions;
11
12
    public function updateConfig()
13
    {
14
        $this->onlyAdmin();
15
        if (!$this->container->getRequest()->isPost()) {
16
            throw new JsonException('Call is not post');
17
        }
18
        $result = array();
19
        $result['successId'] = [];
20
        $result['errorId'] = [];
21
        $configUpdateJson = $this->container->getRequest()->getData('config-update');
22
        $configUpdate = json_decode($configUpdateJson);
23
24
        $configModel = new ConfigModel($this->container);
25
26
        $success = true;
27
        foreach ($configUpdate as $update){
28
29
30
            if (!$configModel->updateConfig($update->dbId, $update->value)) {
31
                $success = false;
32
                $result['errorId'][] = $update->id;
33
            }else{
34
                $result['successId'][] = $update->id;
35
            }
36
        }
37
        $result['success']=$success;
38
39
        echo json_encode($result);
40
41
42
        die();
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
43
        $configModel = new ConfigModel($this->container);
0 ignored issues
show
Unused Code introduced by
$configModel = new App\M...Model($this->container) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
44
        $posts = $this->container->getRequest()->getDataFull();
45
46
        $success = true;
47
        foreach ($posts as $key => $config) {
48
            $configId = $this->removeFromBeginning($key, 'config-');
49
            if (!$configModel->updateConfig($configId, $config)) {
50
                $success = false;
51
            }
52
        }
53
        if ($success) {
54
            $this->alertBox->setAlert('Configuration updates successfully');
55
        } else {
56
            $this->alertBox->setAlert('error in configuration update', 'error');
57
        }
58
        $this->container->getResponse()->redirect('admin/config');
59
    }
60
}