Passed
Push — master ( ead1f2...80923f )
by Andrea
15:57
created

GenerateFormCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 2.0014

Importance

Changes 0
Metric Value
cc 2
eloc 28
nc 2
nop 2
dl 0
loc 46
ccs 26
cts 28
cp 0.9286
crap 2.0014
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\PannelloAmministrazioneBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Filesystem\Filesystem;
11
use Cdf\PannelloAmministrazioneBundle\Utils\ProjectPath;
12
use Cdf\PannelloAmministrazioneBundle\Utils\Utility;
13
use Doctrine\Common\Persistence\ObjectManager;
14
15
class GenerateFormCommand extends Command
16
{
17
    protected static $defaultName = 'pannelloamministrazione:generateformcrud';
18
19
    protected $apppaths;
20
    protected $em;
21
    protected $pammutils;
22
    private $generatemplate;
23
    private $kernel;
24
25 1
    protected function configure()
26
    {
27
        $this
28 1
                ->setDescription('Genera le views per il crud')
29 1
                ->setHelp('Genera le views per il crud, <br/>bi.mwb AppBundle default [--schemaupdate]<br/>')
30 1
                ->addArgument('entityform', InputArgument::REQUIRED, 'Il nome entity del form da creare')
31 1
                ->addOption('generatemplate', InputOption::VALUE_OPTIONAL);
32 1
    }
33
34 1
    public function __construct($kernel, ProjectPath $projectpath, Utility $pammutils, ObjectManager $em)
35
    {
36 1
        $this->kernel = $kernel;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
37 1
        $this->apppaths = $projectpath;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
38 1
        $this->pammutils = $pammutils;
39 1
        $this->em = $em;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
40
41
        // you *must* call the parent constructor
42 1
        parent::__construct();
43 1
    }
44
45 1
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47 1
        set_time_limit(0);
48
49 1
        $bundlename = 'App';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
50 1
        $entityform = $input->getArgument('entityform');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
51 1
        $this->generatemplate = $input->getOption('generatemplate');
52
53 1
        $command = $this->apppaths->getConsole().' --env=dev'.' make:form '.$entityform.'Type '.$entityform;
0 ignored issues
show
Bug introduced by
Are you sure $entityform of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $command = $this->apppaths->getConsole().' --env=dev'.' make:form './** @scrutinizer ignore-type */ $entityform.'Type '.$entityform;
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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 1
        $resultcrud = $this->pammutils->runCommand($command);
55 1
        if (0 == $resultcrud['errcode']) {
56 1
            $fs = new Filesystem();
57
            //Controller
58 1
            $controlleFile = $this->apppaths->getSrcPath().'/Controller/'.$entityform.'Controller.php';
59
60 1
            $formFile = $this->apppaths->getSrcPath().'/Form/'.$entityform.'Type.php';
61
62 1
            $lines = file($formFile, FILE_IGNORE_NEW_LINES);
63
64 1
            array_splice($lines, 8, 0, 'use Symfony\Component\Form\Extension\Core\Type\SubmitType;');
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $input of array_splice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            array_splice(/** @scrutinizer ignore-type */ $lines, 8, 0, 'use Symfony\Component\Form\Extension\Core\Type\SubmitType;');
Loading history...
65
66 1
            array_splice($lines, 14, 0, '        $submitparms = array('
67 1
                    ."'label' => 'Salva','attr' => array(\"class\" => \"btn-outline-primary bisubmit\"));");
68
69 1
            array_splice($lines, 16, 0, "            ->add('submit', SubmitType::class, \$submitparms)");
70
71 1
            array_splice($lines, count($lines) - 3, 0, "            'parametriform' => array()");
72 1
            file_put_contents($formFile, implode("\n", $lines));
73
74 1
            $code = $this->getControllerCode(str_replace('/', '\\', $bundlename), $entityform);
75 1
            $fs->dumpFile($controlleFile, $code);
76 1
            $output->writeln('<info>Creato '.$controlleFile.'</info>');
77
78
            //Routing
79 1
            $retmsg = $this->generateFormRouting($entityform);
80
            //Twig template
81 1
            $this->copyTableStructureWiew($entityform);
82
83 1
            $this->generateFormsDefaultTableValues($entityform);
84 1
            $output->writeln('<info>'.$retmsg.'</info>');
85
86 1
            return 0;
87
        } else {
88
            $output->writeln('<error>'.$resultcrud['message'].'</error>');
89
90
            return 1;
91
        }
92
    }
93
94 1
    private function generateFormRouting($entityform)
95
    {
96
        //Routing del form
97 1
        $bundlename = 'App';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
98 1
        $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...
99 1
        $routingFile = $this->apppaths->getSrcPath().'/../config/routes/'.strtolower($entityform).'.yml';
100
101 1
        $code = $this->getRoutingCode(str_replace('/', '', $bundlename), $entityform);
102 1
        $fs->dumpFile($routingFile, $code);
103
104 1
        $dest = $this->apppaths->getSrcPath().'/../config/routes.yaml';
105
106 1
        $routingContext = str_replace('/', '', $bundlename).'_'.$entityform.':'."\n".
107 1
                '  resource: routes/'.strtolower($entityform).'.yml'."\n".
108 1
                '  prefix: /'.$entityform."\n\n";
109
110
        //Si fa l'append nel file routing del bundle per aggiungerci le rotte della tabella che stiamo gestendo
111 1
        $fh = file_get_contents($dest);
112 1
        if (false !== $fh) {
113 1
            file_put_contents($dest, $routingContext.$fh);
114 1
            $retmsg = 'Routing '.$dest." generato automaticamente da pannelloammonistrazionebundle\n\n* * * * CLEAR CACHE * * * *\n";
115
        } else {
116
            $retmsg = 'Impossibile generare il ruoting automaticamente da pannelloammonistrazionebundle\n';
117
        }
118
119 1
        return $retmsg;
120
    }
121
122 1
    private function copyTableStructureWiew($entityform)
123
    {
124 1
        $fs = new Filesystem();
125
        /* $publicfolder = $this->apppaths->getPublicPath();
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
126
127
          if (!$fs->exists($publicfolder . "/js")) {
128
          $fs->mkdir($publicfolder . "/js", 0777);
129
          }
130
131
          if (!$fs->exists($publicfolder . "/css")) {
132
          $fs->mkdir($publicfolder . "/css", 0777);
133
          } */
134
135 1
        $templatetablefolder = $this->apppaths->getTemplatePath().DIRECTORY_SEPARATOR.$entityform;
136 1
        $crudfolder = $this->kernel->locateResource('@BiCoreBundle')
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...
137 1
                .DIRECTORY_SEPARATOR.'Resources/views/Standard/Crud';
138 1
        $tabellafolder = $this->kernel->locateResource('@BiCoreBundle')
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...
139 1
                .DIRECTORY_SEPARATOR.'Resources/views/Standard/Tabella';
140
141 1
        $fs->mirror($crudfolder, $templatetablefolder.'/Crud');
142 1
        if ($this->generatemplate) {
143
            $fs->mirror($tabellafolder, $templatetablefolder.'/Tabella');
144
        }
145
146
        //$fs->touch($publicfolder . DIRECTORY_SEPARATOR . "js" . DIRECTORY_SEPARATOR . $entityform . ".js");
147
        //$fs->touch($publicfolder . DIRECTORY_SEPARATOR . "css" . DIRECTORY_SEPARATOR . $entityform . ".css");
148 1
    }
149
150 1
    private function generateFormsDefaultTableValues($entityform)
151
    {
152
        //Si inserisce il record di default nella tabella permessi
153 1
        $ruoloAmm = $this->em->getRepository('BiCoreBundle:Ruoli')->findOneBy(array('superadmin' => true)); //SuperAdmin
154
155 1
        $newPermesso = new \Cdf\BiCoreBundle\Entity\Permessi();
156 1
        $newPermesso->setCrud('crud');
157 1
        $newPermesso->setModulo($entityform);
158 1
        $newPermesso->setRuoli($ruoloAmm);
159 1
        $this->em->persist($newPermesso);
160 1
        $this->em->flush();
161
162 1
        $tabelle = new \Cdf\BiCoreBundle\Entity\Colonnetabelle();
163 1
        $tabelle->setNometabella($entityform);
164 1
        $this->em->persist($tabelle);
165 1
        $this->em->flush();
166 1
    }
167
168 1
    private function getControllerCode($bundlename, $tabella)
169
    {
170
        $codeTemplate = <<<EOF
171 1
<?php
172
namespace [bundle]\Controller;
173
174
use Symfony\Component\HttpFoundation\Request;
175
use Symfony\Component\HttpFoundation\Response;
176
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
177
use Cdf\BiCoreBundle\Controller\FiController;
178
use Cdf\BiCoreBundle\Utils\Tabella\ParametriTabella;
179
use [bundle]\Entity\[tabella];
180
use [bundle]\Form\[tabella]Type;
181
                
182
/**
183
* [tabella] controller.
184
*
185
*/
186
187
class [tabella]Controller extends FiController {
188
189
}
190
EOF;
191 1
        $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...
192 1
        $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...
193
194 1
        return $code;
195
    }
196
197 1
    private function getRoutingCode($bundlename, $tabella)
198
    {
199
        $codeTemplate = <<<'EOF'
200 1
[tabella]_container:
201
    path:  /
202
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::index' }
203
204
[tabella]_lista:
205
    path:  /lista
206
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::lista' }
207
    options:
208
        expose: true
209
210
[tabella]_indexdettaglio:
211
    path:  /indexDettaglio
212
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::indexDettaglio' }
213
214
[tabella]_new:
215
    path:  /new
216
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::new' }
217
    requirements: { methods: get|post }
218
219
[tabella]_edit:
220
    path:  /{id}/edit
221
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::edit' }
222
223
[tabella]_update:
224
    path:  /{id}/update
225
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::update' }
226
    requirements: { methods: post|put }
227
228
[tabella]_aggiorna:
229
    path:  /{id}/{token}/aggiorna
230
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::aggiorna' }
231
    requirements: { methods: post|put }
232
    options:
233
        expose: true
234
235
[tabella]_delete:
236
    path:  /{id}/{token}/delete
237
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::delete' }
238
    requirements: { methods: post|delete }
239
240
[tabella]_deletemultiple:
241
    path:  /{token}/delete
242
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::delete' }
243
    requirements: { methods: post|delete }
244
245
[tabella]_tabella:
246
    path:  /tabella
247
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::tabella' }
248
    requirements: { methods: post }
249
EOF;
250 1
        $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...
251 1
        $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...
252
253 1
        return $code;
254
    }
255
}
256