Passed
Push — master ( 4b682e...929ece )
by Andrea
18:49
created

GenerateFormCommand::getRoutingCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 0
cts 5
cp 0
rs 9.2258
c 0
b 0
f 0
cc 1
eloc 46
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Finder\Finder;
11
use Fi\OsBundle\DependencyInjection\OsFunctions;
12
13
class GenerateFormCommand extends ContainerAwareCommand
14
{
15
16
    protected $apppaths;
17
    protected $genhelper;
18
    protected $pammutils;
19
20 1
    protected function configure()
21
    {
22 1
        $this
23 1
                ->setName('pannelloamministrazione:generateformcrud')
24 1
                ->setDescription('Genera le views per il crud')
25 1
                ->setHelp('Genera le views per il crud, <br/>fifree.mwb Fi/CoreBundle default [--schemaupdate]<br/>')
26 1
                ->addArgument('bundlename', InputArgument::REQUIRED, 'Nome del bundle, Fi/CoreBundle')
27 1
                ->addArgument('entityform', InputArgument::REQUIRED, 'Il nome entity del form da creare');
28 1
    }
29
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        set_time_limit(0);
33
        $this->apppaths = $this->getContainer()->get("pannelloamministrazione.projectpath");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal pannelloamministrazione.projectpath does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
34
        $pammutils = $this->getContainer()->get("pannelloamministrazione.utils");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal pannelloamministrazione.utils does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
35
36
        $bundlename = $input->getArgument('bundlename');
37
        $entityform = $input->getArgument('entityform');
38
39
        $crudparms = str_replace('/', '', $bundlename) . ':' . $entityform . ' --route-prefix=' . $entityform
40
                . ' --env=' . $this->getContainer()->get('kernel')->getEnvironment()
41
                . ' --with-write --format=yml --no-interaction'; // --no-debug
42
43
        $phpPath = OsFunctions::getPHPExecutableFromPath();
44
45
        $resultcrud = $pammutils->runCommand($phpPath . ' ' . $this->apppaths->getConsole() . ' doctrine:generate:crud ' . $crudparms);
46
47
        if ($resultcrud['errcode'] == 0) {
48
            $fs = new Filesystem();
49
            //Controller
50
            $controlleFile = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR .
51
                    $bundlename . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR .
52
                    $entityform . 'Controller.php';
53
            $code = $this->getControllerCode(str_replace('/', '\\', $bundlename), $entityform);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
            $fs->dumpFile($controlleFile, $code);
55
            $output->writeln("<info>Creato " . $controlleFile . "</info>");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal <info>Creato does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal </info> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
56
57
            //Routing
58
            $retmsg = $this->generateFormRouting($bundlename, $entityform);
59
            //Twig template (Crea i template per new edit show)
60
            $this->generateFormWiew($bundlename, $entityform, 'edit');
61
            $this->generateFormWiew($bundlename, $entityform, 'index');
62
            $this->generateFormWiew($bundlename, $entityform, 'new');
63
            $appviews = $this->apppaths->getAppPath() . DIRECTORY_SEPARATOR . 'Resources'
64
                    . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . strtolower($entityform);
65
66
            $fs->remove($appviews);
67
            $output->writeln("<info>Rimosso " . $appviews . "</info>");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal <info>Rimosso does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal </info> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
68
69
            $this->generateFormsDefaultTableValues($entityform);
70
            $output->writeln("<info>" . $retmsg . "</info>");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal <info> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal </info> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
71
            return 0;
72
        } else {
73
            $output->writeln("<error>" . $resultcrud['errmsg'] . "</error>");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal <error> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal </error> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
74
            return 1;
75
        }
76
    }
77
78
    private function generateFormRouting($bundlename, $entityform)
79
    {
80
        //Routing del form
81
        $fs = new Filesystem();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
82
        $routingFile = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename .
83
                DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' .
84
                DIRECTORY_SEPARATOR . 'routing' . DIRECTORY_SEPARATOR . strtolower($entityform) . '.yml';
85
86
        $code = $this->getRoutingCode(str_replace('/', '', $bundlename), $entityform);
87
        $fs->dumpFile($routingFile, $code);
88
89
        //Fixed: Adesso questa parte la fa da solo symfony (05/2015)
90
        //Refixed dalla versione 2.8 non lo fa più (04/2016)
91
92
        $dest = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
93
                'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'routing.yml';
94
95
        $routingContext = "\n" . str_replace('/', '', $bundlename) . '_' . $entityform . ': ' . "\n" .
96
                '  resource: "@' . str_replace('/', '', $bundlename) . '/Resources/config/routing/' . strtolower($entityform) . '.yml"' . "\n" .
97
                '  prefix: /' . $entityform . "\n";
98
99
        //Si fa l'append nel file routing del bundle per aggiungerci le rotte della tabella che stiamo gestendo
100
        $fh = fopen($dest, 'a');
101
        if ($fh !== false) {
102
            fwrite($fh, $routingContext);
103
            fclose($fh);
104
            $retmsg = 'Routing ' . $dest . " generato automaticamente da pannelloammonistrazionebundle\n\n* * * * CLEAR CACHE * * * *\n";
105
        } else {
106
            $retmsg = 'Impossibile generare il ruoting automaticamente da pannelloammonistrazionebundle\n';
107
        }
108
109
        return $retmsg;
110
    }
111
112
    private function generateFormWiew($bundlename, $entityform, $view)
113
    {
114
        $fs = new Filesystem();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
115
        $folderview = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
116
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR .
117
                $entityform . DIRECTORY_SEPARATOR;
118
        $dest = $folderview . $view . '.html.twig';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
119
        $fs->mkdir($folderview);
120
        file_put_contents($dest, "{% include 'FiCoreBundle:Standard:" . $view . ".html.twig' %}");
121
    }
122
123
    private function generateFormsDefaultTableValues($entityform)
124
    {
125
        //Si inserisce il record di default nella tabella permessi
126
        $em = $this->getContainer()->get('doctrine')->getManager();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
127
        $ruoloAmm = $em->getRepository('FiCoreBundle:Ruoli')->findOneBy(array('is_superadmin' => true)); //SuperAdmin
128
129
        $newPermesso = new \Fi\CoreBundle\Entity\Permessi();
130
        $newPermesso->setCrud('crud');
131
        $newPermesso->setModulo($entityform);
132
        $newPermesso->setRuoli($ruoloAmm);
133
        $em->persist($newPermesso);
134
        $em->flush();
135
136
        $tabelle = new \Fi\CoreBundle\Entity\Tabelle();
137
        $tabelle->setNometabella($entityform);
138
        $em->persist($tabelle);
139
        $em->flush();
140
    }
141
142
    private function getControllerCode($bundlename, $tabella)
143
    {
144
        $codeTemplate = <<<EOF
145
<?php
146
namespace [bundle]\Controller;
147
148
use Fi\CoreBundle\Controller\FiController;
149
use Symfony\Component\HttpFoundation\Request;
150
use Symfony\Component\HttpFoundation\Response;
151
use Fi\CoreBundle\Controller\Griglia;
152
use [bundle]\Entity\[tabella];
153
use [bundle]\Form\[tabella]Type;
154
155
156
/**
157
* [tabella] controller.
158
*
159
*/
160
161
class [tabella]Controller extends FiController {
162
163
}
164
EOF;
165
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
166
        $code = str_replace('[tabella]', $tabella, $codebundle);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
167
168
        return $code;
169
    }
170
171
    private function getRoutingCode($bundlename, $tabella)
172
    {
173
        $codeTemplate = <<<'EOF'
174
[tabella]_container:
175
    path:  /
176
    defaults: { _controller: "[bundle]:[tabella]:index" }
177
178
[tabella]_new:
179
    path:  /new
180
    defaults: { _controller: "[bundle]:[tabella]:new" }
181
182
[tabella]_create:
183
    path:  /create
184
    defaults: { _controller: "[bundle]:[tabella]:create" }
185
    requirements: { methods: post }
186
187
[tabella]_edit:
188
    path:  /{id}/edit
189
    defaults: { _controller: "[bundle]:[tabella]:edit" }
190
191
[tabella]_update:
192
    path:  /{id}/update
193
    defaults: { _controller: "[bundle]:[tabella]:update" }
194
    requirements: { methods: post|put }
195
196
[tabella]_aggiorna:
197
    path:  /aggiorna
198
    defaults: { _controller: "[bundle]:[tabella]:aggiorna" }
199
    requirements: { methods: post|put }
200
201
[tabella]_delete:
202
    path:  /{id}/delete
203
    defaults: { _controller: "[bundle]:[tabella]:delete" }
204
    requirements: { methods: post|delete }
205
206
[tabella]_deletemultiple:
207
    path:  /delete
208
    defaults: { _controller: "[bundle]:[tabella]:delete" }
209
    requirements: { methods: post|delete }
210
211
[tabella]_griglia:
212
    path:  /griglia
213
    defaults: { _controller: "[bundle]:[tabella]:Griglia" }
214
    requirements: { methods: get|post }
215
EOF;
216
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
217
        $code = str_replace('[tabella]', $tabella, $codebundle);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
218
219
        return $code;
220
    }
221
}
222