AppearanceController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 5 1
A index() 0 9 2
A save() 0 19 2
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2022 Atlas Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\Controller\Admin;
14
15
use App\Utility\ApiConfigTrait;
16
use BEdita\SDK\BEditaClientException;
17
use Cake\Core\Configure;
18
use Cake\Http\Response;
19
use Cake\Utility\Hash;
20
use Cake\Utility\Inflector;
21
22
/**
23
 * Appearance Controller
24
 *
25
 * @property \App\Controller\Component\ConfigComponent $Config
26
 */
27
class AppearanceController extends AdministrationBaseController
28
{
29
    use ApiConfigTrait;
30
31
    /**
32
     * Resource type in use
33
     *
34
     * @var string
35
     */
36
    protected $resourceType = 'config';
37
38
    /**
39
     * @inheritDoc
40
     */
41
    protected $readonly = false;
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function initialize(): void
47
    {
48
        parent::initialize();
49
50
        $this->Security->setConfig('unlockedActions', ['save']);
51
    }
52
53
    /**
54
     * Index method
55
     *
56
     * @return \Cake\Http\Response|null
57
     */
58
    public function index(): ?Response
59
    {
60
        $configs = [];
61
        foreach (static::$configKeys as $property) {
62
            $configs[Inflector::underscore($property)] = Configure::read($property);
63
        }
64
        $this->set('configs', $configs);
65
66
        return null;
67
    }
68
69
    /**
70
     * Save data
71
     *
72
     * @return \Cake\Http\Response|null
73
     */
74
    public function save(): ?Response
75
    {
76
        $this->getRequest()->allowMethod(['post']);
77
        $this->viewBuilder()->setClassName('Json');
78
        try {
79
            $data = (array)$this->getRequest()->getData();
80
            $propertyName = (string)Hash::get($data, 'property_name');
81
            $propertyValue = (array)Hash::get($data, 'property_value');
82
            $this->saveApiConfig(Inflector::camelize($propertyName), $propertyValue);
83
            $response = 'Configuration saved';
84
            $this->set('response', $response);
85
            $this->setSerialize(['response']);
86
        } catch (BEditaClientException $e) {
87
            $error = $e->getMessage();
88
            $this->set('error', $error);
89
            $this->setSerialize(['error']);
90
        }
91
92
        return null;
93
    }
94
}
95