Passed
Push — Security_and_bug_fixes ( cb92f3...2b89b6 )
by Stone
03:26
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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\Container;
8
use Core\JsonException;
9
use Core\Traits\StringFunctions;
10
11
class Config extends AjaxController
12
{
13
    use StringFunctions;
14
15
    private $configModel;
16
17
    public function __construct(Container $container)
18
    {
19
        parent::__construct($container);
20
        $this->configModel = new ConfigModel($this->container);
21
    }
22
23
    /**
24
     * Update the site configuration via Ajax post
25
     * @throws JsonException
26
     */
27
    public function update()
28
    {
29
        //security checks
30
        $this->onlyAdmin();
31
        $this->onlyPost();
32
        //preparing our return results
33
        $result = array();
34
        $result['successId'] = [];
35
        $result['errorId'] = [];
36
        $result['success'] = true;
37
        $configUpdateJson = $this->container->getRequest()->getData('config-update');
38
        $configUpdate = json_decode($configUpdateJson);
39
40
        foreach ($configUpdate as $update) {
41
42
43
            if (!$this->configModel->updateConfig($update->name, $update->value)) {
44
                $result['success'] = false;
45
                $result['errorId'][] = $update->name;
46
            } else {
47
                $result['successId'][] = $update->name;
48
            }
49
        }
50
51
        echo json_encode($result);
52
    }
53
}