Passed
Push — master ( 2955c9...ee5866 )
by Fran
05:26
created

ConfigController::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 9
cp 0
crap 2
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\controller\base\Admin;
10
11
/**
12
 * Class ConfigController
13
 * @package PSFS\controller
14
 */
15
class ConfigController extends Admin
16
{
17
18
    /**
19
     * Servicio que devuelve los parámetros disponibles
20
     * @GET
21
     * @route /admin/config/params
22
     * @visible false
23
     * @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...
24
     */
25
    public function getConfigParams() {
26
        $response = array_merge(Config::$required, Config::$optional);
27
        $domains = Router::getInstance()->getDomains();
28
        foreach($domains as $domain => $routes) {
0 ignored issues
show
Bug introduced by
The expression $domains of type array|string|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
29
            $pDomain = str_replace('@', '', $domain);
30
            $pDomain = str_replace('/', '', $pDomain);
31
            $response[] = strtolower($pDomain) . '.api.secret';
32
        }
33
        return $this->json($response);
34
    }
35
36
    /**
37
     * Método que gestiona la configuración de las variables
38
     * @GET
39
     * @Route /admin/config
40
     * @return string|null
41
     * @throws \HttpException
42
     */
43
    public function config()
44
    {
45
        Logger::log("Config loaded executed by " . $this->getRequest()->getRequestUri());
46
        /* @var $form \PSFS\base\config\ConfigForm */
47
        $form = new ConfigForm();
48
        $form->build();
49
        return $this->render('welcome.html.twig', array(
50
            'text' => _("Bienvenido a PSFS"),
51
            'config' => $form,
52
            'typeahead_data' => array_merge(Config::$required, Config::$optional),
53
        ));
54
    }
55
56
    /**
57
     * Servicio que guarda la configuración de la plataforma
58
     * @POST
59
     * @route /admin/config
60
     * @visible false
61
     * @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...
62
     * @throws \HttpException
63
     */
64
    public function saveConfig()
65
    {
66
        Logger::getInstance()->infoLog(_("Guardando configuración"));
67
        /* @var $form \PSFS\base\config\ConfigForm */
68
        $form = new ConfigForm();
69
        $form->build();
70
        $form->hydrate();
71
        if ($form->isValid()) {
72
            $debug = Config::getInstance()->getDebugMode();
73
            $newDebug = $form->getFieldValue("debug");
74
            if (Config::save($form->getData(), $form->getExtraData())) {
75
                Logger::log(_('Configuración guardada correctamente'));
76
                //Verificamos si tenemos que limpiar la cache del DocumentRoot
77
                if (boolval($debug) !== boolval($newDebug)) {
78
                    Config::clearDocumentRoot();
79
                }
80
                Security::getInstance()->setFlash("callback_message", _("Configuración actualizada correctamente"));
81
                Security::getInstance()->setFlash("callback_route", $this->getRoute("admin-config", true));
82
            } else {
83
                throw new \HttpException(_('Error al guardar la configuración, prueba a cambiar los permisos'), 403);
84
            }
85
        }
86
        return $this->render('welcome.html.twig', array(
87
            'text' => _("Bienvenido a PSFS"),
88
            'config' => $form,
89
            'typeahead_data' => array_merge(Config::$required, Config::$optional),
90
        ));
91
    }
92
}