Passed
Push — master ( f113c6...ab5b91 )
by Fran
02:46
created

ConfigController::saveConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 26
ccs 0
cts 20
cp 0
crap 20
rs 9.6
1
<?php
2
3
namespace PSFS\controller;
4
5
use HttpException;
6
use PSFS\base\config\Config;
7
use PSFS\base\config\ConfigForm;
8
use PSFS\base\dto\JsonResponse;
9
use PSFS\base\exception\ConfigException;
10
use PSFS\base\exception\FormException;
11
use PSFS\base\Logger;
12
use PSFS\base\Router;
13
use PSFS\base\Security;
14
use PSFS\base\types\helpers\DeployHelper;
15
use PSFS\base\types\helpers\GeneratorHelper;
16
use PSFS\controller\base\Admin;
17
18
/**
19
 * Class ConfigController
20
 * @package PSFS\controller
21
 */
22
class ConfigController extends Admin
23
{
24
25
    /**
26
     * Servicio que devuelve los parámetros disponibles
27
     * @GET
28
     * @route /admin/config/params
29
     * @label Parámetros de configuración de PSFS
30
     * @visible false
31
     * @return JsonResponse(data=array)
32
     */
33 1
    public function getConfigParams()
34
    {
35 1
        $response = array_merge(Config::$required, Config::$optional);
36 1
        $domains = Router::getInstance()->getDomains();
37 1
        foreach ($domains as $domain => $routes) {
38 1
            $pDomain = str_replace('@', '', $domain);
39 1
            $pDomain = str_replace('/', '', $pDomain);
40 1
            $response[] = strtolower($pDomain) . '.api.secret';
41
        }
42 1
        return $this->json($response);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->json($response) returns the type string which is incompatible with the documented return type PSFS\base\dto\JsonResponse.
Loading history...
43
    }
44
45
    /**
46
     * Método que gestiona la configuración de las variables
47
     * @GET
48
     * @Route /admin/config
49
     * @label Configuración general
50
     * @icon fa-cogs
51
     * @return string HTML
52
     * @throws FormException
53
     */
54 1
    public function config()
55
    {
56 1
        Logger::log("Config loaded executed by " . $this->getRequest()->getRequestUri());
57 1
        if (defined('PSFS_UNIT_TESTING_EXECUTION')) {
58 1
            throw new ConfigException('CONFIG');
59
        }
60
        $form = new ConfigForm(Router::getInstance()->getRoute('admin-config'), Config::$required, Config::$optional, Config::getInstance()->dumpConfig());
61
        $form->build();
62
        return $this->render('welcome.html.twig', array(
63
            'text' => t("Bienvenido a PSFS"),
64
            'config' => $form,
65
            'typeahead_data' => array_merge(Config::$required, Config::$optional),
66
        ));
67
    }
68
69
    /**
70
     * Servicio que guarda la configuración de la plataforma
71
     * @POST
72
     * @route /admin/config
73
     * @visible false
74
     * @return string
75
     * @throws FormException|HttpException
76
     */
77
    public function saveConfig()
78
    {
79
        Logger::log(t("Guardando configuración"), LOG_INFO);
80
        $form = new ConfigForm(Router::getInstance()->getRoute('admin-config'), Config::$required, Config::$optional, Config::getInstance()->dumpConfig());
81
        $form->build();
82
        $form->hydrate();
83
        if ($form->isValid()) {
84
            $debug = Config::getInstance()->getDebugMode();
85
            $newDebug = $form->getFieldValue("debug");
86
            if (Config::save($form->getData(), $form->getExtraData())) {
87
                Logger::log(t('Configuración guardada correctamente'));
88
                //Verificamos si tenemos que limpiar la cache del DocumentRoot
89
                if (boolval($debug) !== boolval($newDebug)) {
90
                    DeployHelper::updateCacheVar();
91
                    GeneratorHelper::clearDocumentRoot();
92
                }
93
                Security::getInstance()->setFlash("callback_message", t("Configuración actualizada correctamente"));
94
                Security::getInstance()->setFlash("callback_route", $this->getRoute("admin-config", true));
95
            } else {
96
                throw new HttpException(t('Error al guardar la configuración, prueba a cambiar los permisos'), 403);
97
            }
98
        }
99
        return $this->render('welcome.html.twig', array(
100
            'text' => t("Bienvenido a PSFS"),
101
            'config' => $form,
102
            'typeahead_data' => array_merge(Config::$required, Config::$optional),
103
        ));
104
    }
105
}
106