Passed
Push — master ( a15eb9...7b437d )
by Fran
04:59
created

GeneratorController::createRoot()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 24
nop 2
dl 0
loc 35
ccs 0
cts 17
cp 0
crap 42
rs 8.439
c 0
b 0
f 0
1
<?php
2
namespace PSFS\controller;
3
4
use PSFS\base\config\ModuleForm;
5
use PSFS\base\Logger;
6
use PSFS\base\Security;
7
use PSFS\base\Template;
8
use PSFS\base\types\helpers\GeneratorHelper;
9
use PSFS\controller\base\Admin;
10
use PSFS\Services\GeneratorService;
11
use Symfony\Component\Console\Output\ConsoleOutput;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Class GeneratorController
16
 * @package PSFS\controller
17
 * @domain ROOT
18
 */
19
class GeneratorController extends Admin
20
{
21
    /**
22
     * @Inyectable
23
     * @var  \PSFS\services\GeneratorService Servicio de generación de estructura de directorios
24
     */
25
    protected $gen;
26
27
    /**
28
     * Método que genera un nuevo módulo
29
     * @GET
30
     * @route /admin/module
31
     *
32
     * @return string HTML
33
     * @throws \HttpException
34
     */
35
    public function generateModule()
36
    {
37
        Logger::log("Arranque generador de módulos al solicitar " . $this->getRequest()->getRequestUri());
38
        /* @var $form \PSFS\base\config\ConfigForm */
39
        $form = new ModuleForm();
40
        $form->build();
41
        return $this->render("modules.html.twig", array(
42
            'form' => $form,
43
        ));
44
    }
45
46
    /**
47
     * @POST
48
     * @route /admin/module
49
     * @label Generador de módulos
50
     * @return string
51
     */
52
    public function doGenerateModule()
53
    {
54
        $form = new ModuleForm();
55
        $form->build();
56
        $form->hydrate();
57
        if ($form->isValid()) {
58
            $module = $form->getFieldValue("module");
59
            $type = $form->getFieldValue("controllerType");
60
            $apiClass = $form->getFieldValue("api");
61
            try {
62
                $module = preg_replace('/(\\\|\/)/', '/', $module);
63
                $module = preg_replace('/^\//', '', $module);
64
                GeneratorHelper::checkCustomNamespaceApi($apiClass);
65
                $this->gen->createStructureModule($module, false, $type, $apiClass);
0 ignored issues
show
Bug introduced by
It seems like $module defined by preg_replace('/^\\//', '', $module) on line 63 can also be of type array<integer,string>; however, PSFS\Services\GeneratorS...createStructureModule() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
66
                Security::getInstance()->setFlash("callback_message", str_replace("%s", $module, _("Módulo %s generado correctamente")));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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...
67
                Security::getInstance()->setFlash("callback_route", $this->getRoute("admin-module", true));
68
            } catch (\Exception $e) {
69
                Logger::getInstance()->infoLog($e->getMessage() . " [" . $e->getFile() . ":" . $e->getLine() . "]");
70
                Security::getInstance()->setFlash("callback_message", $e->getMessage());
71
            }
72
        }
73
        return $this->render("modules.html.twig", array(
74
            'form' => $form,
75
        ));
76
    }
77
78
    public static function createRoot($path = WEB_DIR, OutputInterface $output = null) {
79
80
        if(null === $output) {
81
            $output = new ConsoleOutput();
82
        }
83
84
        GeneratorHelper::createDir($path);
85
        $paths = array("js", "css", "img", "media", "font");
86
        foreach ($paths as $htmlPath) {
87
            GeneratorHelper::createDir($path . DIRECTORY_SEPARATOR . $htmlPath);
88
        }
89
90
        // Generates the root needed files
91
        $files = [
92
            'index' => 'index.php',
93
            'browserconfig' => 'browserconfig.xml',
94
            'crossdomain' => 'crossdomain.xml',
95
            'humans' => 'humans.txt',
96
            'robots' => 'robots.txt',
97
        ];
98
        foreach ($files as $templates => $filename) {
99
            $text = Template::getInstance()->dump("generator/html/" . $templates . '.html.twig');
100
            if (false === file_put_contents($path . DIRECTORY_SEPARATOR . $filename, $text)) {
101
                $output->writeln('Can\t create the file ' . $filename);
102
            } else {
103
                $output->writeln($filename . ' created successfully');
104
            }
105
        }
106
107
        //Export base locale translations
108
        if (!file_exists(BASE_DIR . DIRECTORY_SEPARATOR . 'locale')) {
109
            GeneratorHelper::createDir(BASE_DIR . DIRECTORY_SEPARATOR . 'locale');
110
            GeneratorService::copyr(SOURCE_DIR . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'locale', BASE_DIR . DIRECTORY_SEPARATOR . 'locale');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 153 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...
111
        }
112
    }
113
}