GenerateFormCommand::getRoutingCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 46
c 3
b 0
f 0
dl 0
loc 49
ccs 5
cts 5
cp 1
rs 9.1781
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\Command;
4
5
use Fi\CoreBundle\Entity\Permessi;
6
use Fi\CoreBundle\Entity\Tabelle;
7
use Fi\OsBundle\DependencyInjection\OsFunctions;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Filesystem\Filesystem;
13
14
class GenerateFormCommand extends ContainerAwareCommand
15
{
16
17
    protected $apppaths;
18
    protected $genhelper;
19
    protected $pammutils;
20
21 6
    protected function configure()
22
    {
23
        $this
24 6
                ->setName('pannelloamministrazione:generateformcrud')
25 6
                ->setDescription('Genera le views per il crud')
26 6
                ->setHelp('Genera le views per il crud, <br/>fifree.mwb Fi/CoreBundle default [--schemaupdate]<br/>')
27 6
                ->addArgument('entityform', InputArgument::REQUIRED, 'Il nome entity del form da creare');
28 6
    }
29 2
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31 2
        set_time_limit(0);
32 2
        $this->apppaths = $this->getContainer()->get("pannelloamministrazione.projectpath");
33 2
        $pammutils = $this->getContainer()->get("pannelloamministrazione.utils");
34
35 2
        $bundlename = "App";
36 2
        $entityform = $input->getArgument('entityform');
37
38 2
        $phpPath = OsFunctions::getPHPExecutableFromPath();
39 2
        $command = $phpPath . ' ' . $this->apppaths->getConsole() . ' --env=dev make:form ';
0 ignored issues
show
Bug introduced by
The method getConsole() does not exist on null. ( Ignorable by Annotation )

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

39
        $command = $phpPath . ' ' . $this->apppaths->/** @scrutinizer ignore-call */ getConsole() . ' --env=dev make:form ';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40 2
        $resultcrud = $pammutils->runCommand($command . $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

40
        $resultcrud = $pammutils->runCommand($command . /** @scrutinizer ignore-type */ $entityform . "Type" . " " . $entityform);
Loading history...
41 2
        if ($resultcrud['errcode'] == 0) {
42 2
            $fs = new Filesystem();
43
            //Controller
44 2
            $controlleFile = $this->apppaths->getSrcPath() . '/Controller/' . $entityform . 'Controller.php';
45 2
            $code = $this->getControllerCode(str_replace('/', '\\', $bundlename), $entityform);
46 2
            $fs->dumpFile($controlleFile, $code);
47 2
            $output->writeln("<info>Creato " . $controlleFile . "</info>");
48
49
            //Routing
50 2
            $retmsg = $this->generateFormRouting($bundlename, $entityform);
51
            //Twig template (Crea i template per new edit show)
52 2
            $this->generateFormWiew($bundlename, $entityform, 'edit');
53 2
            $this->generateFormWiew($bundlename, $entityform, 'index');
54 2
            $this->generateFormWiew($bundlename, $entityform, 'new');
55
56 2
            $this->generateFormsDefaultTableValues($entityform);
57 2
            $output->writeln("<info>" . $retmsg . "</info>");
58 2
            return 0;
59
        } else {
60
            $output->writeln("<error>" . $resultcrud['errmsg'] . "</error>");
61
            return 1;
62
        }
63
    }
64 2
    private function generateFormRouting($bundlename, $entityform)
65
    {
66
        //Routing del form
67 2
        $fs = new Filesystem();
68 2
        $routingFile = $this->apppaths->getSrcPath() . '/../config/routes/' . strtolower($entityform) . '.yml';
69
70 2
        $code = $this->getRoutingCode(str_replace('/', '', $bundlename), $entityform);
71 2
        $fs->dumpFile($routingFile, $code);
72
73
        //Fixed: Adesso questa parte la fa da solo symfony (05/2015)
74
        //Refixed dalla versione 2.8 non lo fa più (04/2016)
75
76 2
        $dest = $this->apppaths->getSrcPath() . '/../config/routes.yaml';
77
78 2
        $routingContext = str_replace('/', '', $bundlename) . '_' . $entityform . ':' . "\n" .
79 2
                '  resource: routes/' . strtolower($entityform) . '.yml' . "\n" .
80 2
                '  prefix: /' . $entityform . "\n\n";
81
82
        //Si fa l'append nel file routing del bundle per aggiungerci le rotte della tabella che stiamo gestendo
83 2
        $fh = file_get_contents($dest);
84 2
        if ($fh !== false) {
85 2
            file_put_contents($dest, $routingContext . $fh);
86 2
            $retmsg = 'Routing ' . $dest . " generato automaticamente da pannelloammonistrazionebundle\n\n* * * * CLEAR CACHE * * * *\n";
87
        } else {
88
            $retmsg = 'Impossibile generare il ruoting automaticamente da pannelloammonistrazionebundle\n';
89
        }
90
91 2
        return $retmsg;
92
    }
93 2
    private function generateFormWiew($bundlename, $entityform, $view)
0 ignored issues
show
Unused Code introduced by
The parameter $bundlename is not used and could be removed. ( Ignorable by Annotation )

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

93
    private function generateFormWiew(/** @scrutinizer ignore-unused */ $bundlename, $entityform, $view)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95 2
        $fs = new Filesystem();
96 2
        $folderview = $this->apppaths->getSrcPath() . '/../templates/' . $entityform . DIRECTORY_SEPARATOR;
97 2
        $dest = $folderview . $view . '.html.twig';
98 2
        $fs->mkdir($folderview);
99 2
        file_put_contents($dest, "{% include 'FiCoreBundle:Standard:" . $view . ".html.twig' %}");
100 2
    }
101 2
    private function generateFormsDefaultTableValues($entityform)
102
    {
103
        //Si inserisce il record di default nella tabella permessi
104 2
        $em = $this->getContainer()->get('doctrine')->getManager();
105 2
        $ruoloAmm = $em->getRepository('FiCoreBundle:Ruoli')->findOneBy(array('is_superadmin' => true)); //SuperAdmin
106
107 2
        $newPermesso = new Permessi();
108 2
        $newPermesso->setCrud('crud');
109 2
        $newPermesso->setModulo($entityform);
110 2
        $newPermesso->setRuoli($ruoloAmm);
111 2
        $em->persist($newPermesso);
112 2
        $em->flush();
113
114 2
        $tabelle = new Tabelle();
115 2
        $tabelle->setNometabella($entityform);
116 2
        $em->persist($tabelle);
117 2
        $em->flush();
118 2
    }
119 2
    private function getControllerCode($bundlename, $tabella)
120
    {
121
        $codeTemplate = <<<EOF
122 2
<?php
123
namespace [bundle]\Controller;
124
125
use Fi\CoreBundle\Controller\FiController;
126
use Symfony\Component\HttpFoundation\Request;
127
use Symfony\Component\HttpFoundation\Response;
128
use Fi\CoreBundle\Controller\Griglia;
129
use [bundle]\Entity\[tabella];
130
use [bundle]\Form\[tabella]Type;
131
132
133
/**
134
* [tabella] controller.
135
*
136
*/
137
138
class [tabella]Controller extends FiController {
139
140
}
141
EOF;
142 2
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
143 2
        $code = str_replace('[tabella]', $tabella, $codebundle);
144
145 2
        return $code;
146
    }
147 2
    private function getRoutingCode($bundlename, $tabella)
148
    {
149
        $codeTemplate = <<<'EOF'
150 2
[tabella]_container:
151
    path:  /
152
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::indexAction' }
153
154
[tabella]_new:
155
    path:  /new
156
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::newAction' }
157
158
[tabella]_create:
159
    path:  /create
160
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::createAction' }
161
    requirements: { methods: post }
162
163
[tabella]_edit:
164
    path:  /{id}/edit
165
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::editAction' }
166
167
[tabella]_update:
168
    path:  /{id}/update
169
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::updateAction' }
170
    requirements: { methods: post|put }
171
172
[tabella]_aggiorna:
173
    path:  /aggiorna
174
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::aggiornaAction' }
175
    requirements: { methods: post|put }
176
177
[tabella]_delete:
178
    path:  /{id}/delete
179
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::deleteAction' }
180
    requirements: { methods: post|delete }
181
182
[tabella]_deletemultiple:
183
    path:  /delete
184
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::deleteAction' }
185
    requirements: { methods: post|delete }
186
187
[tabella]_griglia:
188
    path:  /griglia
189
    defaults: { _controller: '[bundle]\Controller\[tabella]Controller::GrigliaAction' }
190
    requirements: { methods: get|post }
191
EOF;
192 2
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
193 2
        $code = str_replace('[tabella]', $tabella, $codebundle);
194
195 2
        return $code;
196
    }
197
}
198