Completed
Push — master ( 196a6a...314b46 )
by Stone
12s
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
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
use Swift_TransportException;
11
12
class Config extends AjaxController
13
{
14
    use StringFunctions;
15
16
    protected $sendMail;
17
18
    private $configModel;
19
20
    public function __construct(Container $container)
21
    {
22
        $this->loadModules[] = 'SendMail';
23
        parent::__construct($container);
24
        $this->configModel = new ConfigModel($this->container);
25
    }
26
27
    /**
28
     * Update the site configuration via Ajax post
29
     * @throws JsonException
30
     */
31
    public function update()
32
    {
33
        //security checks
34
        $this->onlyAdmin();
35
        $this->onlyPost();
36
        //preparing our return results
37
        $result = array();
38
        $result['successId'] = [];
39
        $result['errorId'] = [];
40
        $result['success'] = true;
41
        $configUpdateJson = $this->container->getRequest()->getData('config-update');
42
        $configUpdate = json_decode($configUpdateJson);
43
44
        foreach ($configUpdate as $update) {
45
46
47
            if (!$this->configModel->updateConfig($update->name, $update->value)) {
48
                $result['success'] = false;
49
                $result['errorId'][] = $update->name;
50
            } else {
51
                $result['successId'][] = $update->name;
52
            }
53
        }
54
55
        echo json_encode($result);
56
    }
57
58
    /**
59
     * Send a test mail
60
     * @throws JsonException
61
     */
62
    public function testMail(){
63
        $this->onlyAdmin();
64
        $this->onlyPost();
65
        $result = array();
66
        $result['success'] = false;
67
        try{
68
            $this->sendMail->sendTestMail();
69
            $result['success'] = true;
70
        }catch (Swift_TransportException $e) {
71
            $result['success'] = false;
72
        }
73
74
        echo json_encode($result);
75
    }
76
}