Passed
Push — master ( 445a59...4feda1 )
by Fran
03:49
created

ConfigController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 22.86%

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 8
ccs 8
cts 35
cp 0.2286

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigParams() 0 11 2
B saveConfig() 0 28 4
A config() 0 12 1
1
<?php
2
namespace PSFS\controller;
3
4
use PSFS\base\config\Config;
5
use PSFS\base\config\ConfigForm;
6
use PSFS\base\Logger;
7
use PSFS\base\Router;
8
use PSFS\base\Security;
9
use PSFS\base\types\helpers\GeneratorHelper;
10
use PSFS\controller\base\Admin;
11
12
/**
13
 * Class ConfigController
14
 * @package PSFS\controller
15
 */
16
class ConfigController extends Admin
17
{
18
19
    /**
20
     * Servicio que devuelve los parámetros disponibles
21
     * @GET
22
     * @route /admin/config/params
23
     * @label Parámetros de configuración de PSGS
24
     * @visible false
25
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
26
     */
27
    public function getConfigParams()
28
    {
29
        $response = array_merge(Config::$required, Config::$optional);
30
        $domains = Router::getInstance()->getDomains();
31
        foreach ($domains as $domain => $routes) {
32
            $pDomain = str_replace('@', '', $domain);
33
            $pDomain = str_replace('/', '', $pDomain);
34
            $response[] = strtolower($pDomain) . '.api.secret';
35
        }
36
        return $this->json($response);
37
    }
38
39
    /**
40
     * Método que gestiona la configuración de las variables
41
     * @GET
42
     * @Route /admin/config
43
     * @return string|null
44
     * @throws \HttpException
45
     */
46 1
    public function config()
47
    {
48 1
        Logger::log("Config loaded executed by " . $this->getRequest()->getRequestUri());
49
        /* @var $form \PSFS\base\config\ConfigForm */
50 1
        $form = new ConfigForm(Router::getInstance()->getRoute('admin-config'), Config::$required, Config::$optional, Config::getInstance()->dumpConfig());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 155 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
51 1
        $form->build();
52 1
        return $this->render('welcome.html.twig', array(
53 1
            'text' => _("Bienvenido a PSFS"),
54 1
            'config' => $form,
55 1
            'typeahead_data' => array_merge(Config::$required, Config::$optional),
56
        ));
57
    }
58
59
    /**
60
     * Servicio que guarda la configuración de la plataforma
61
     * @POST
62
     * @route /admin/config
63
     * @visible false
64
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
65
     * @throws \HttpException
66
     */
67
    public function saveConfig()
68
    {
69
        Logger::getInstance()->infoLog(_("Guardando configuración"));
70
        /* @var $form \PSFS\base\config\ConfigForm */
71
        $form = new ConfigForm(Router::getInstance()->getRoute('admin-config'), Config::$required, Config::$optional, Config::getInstance()->dumpConfig());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 155 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
72
        $form->build();
73
        $form->hydrate();
74
        if ($form->isValid()) {
75
            $debug = Config::getInstance()->getDebugMode();
76
            $newDebug = $form->getFieldValue("debug");
77
            if (Config::save($form->getData(), $form->getExtraData())) {
78
                Logger::log(_('Configuración guardada correctamente'));
79
                //Verificamos si tenemos que limpiar la cache del DocumentRoot
80
                if (boolval($debug) !== boolval($newDebug)) {
81
                    GeneratorHelper::clearDocumentRoot();
82
                }
83
                Security::getInstance()->setFlash("callback_message", _("Configuración actualizada correctamente"));
84
                Security::getInstance()->setFlash("callback_route", $this->getRoute("admin-config", true));
85
            } else {
86
                throw new \HttpException(_('Error al guardar la configuración, prueba a cambiar los permisos'), 403);
87
            }
88
        }
89
        return $this->render('welcome.html.twig', array(
90
            'text' => _("Bienvenido a PSFS"),
91
            'config' => $form,
92
            'typeahead_data' => array_merge(Config::$required, Config::$optional),
93
        ));
94
    }
95
}