PannelloAmministrazioneController::indexAction()   B
last analyzed

Complexity

Conditions 9
Paths 64

Size

Total Lines 77
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 9.1975

Importance

Changes 0
Metric Value
eloc 56
c 0
b 0
f 0
dl 0
loc 77
ccs 45
cts 52
cp 0.8654
rs 7.4044
cc 9
nc 64
nop 0
crap 9.1975

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Finder\Finder;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Process\Process;
11
use Fi\OsBundle\DependencyInjection\OsFunctions;
12
use Fi\PannelloAmministrazioneBundle\DependencyInjection\PannelloAmministrazioneUtils;
13
use Symfony\Component\Lock\Factory;
14
use Symfony\Component\Lock\Store\FlockStore;
15
16
class PannelloAmministrazioneController extends Controller
17
{
18
19
    protected $apppaths;
20
    protected $locksystem;
21
    protected $factory;
22
23 1
    public function __construct()
24
    {
25 1
        $store = new FlockStore(sys_get_temp_dir());
26 1
        $factory = new Factory($store);
27 1
        $this->locksystem = $factory->createLock('pannelloamministrazione-command');
28 1
        $this->locksystem->release();
29 1
    }
30
31 1
    public function indexAction()
32
    {
33 1
        $finder = new Finder();
34 1
        $fs = new Filesystem();
35 1
        $this->apppaths = $this->get("pannelloamministrazione.projectpath");
36
37 1
        $projectDir = $this->apppaths->getRootPath();
38 1
        $bundlelists = $this->getParameter('kernel.bundles');
39 1
        $bundles = array();
40 1
        foreach ($bundlelists as $bundle) {
41 1
            if (substr($bundle, 0, 2) === 'Fi') {
42 1
                $bundle = str_replace('\\', '/', $bundle);
43 1
                $bundlepath = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . substr($bundle, 0, strripos($bundle, '/'));
44 1
                if ($fs->exists($bundlepath)) {
45
                    $bundles[] = substr($bundle, 0, strripos($bundle, '/'));
46
                }
47
            }
48
        }
49 1
        $docDir = $this->apppaths->getDocPath();
50 1
        $themes = array("sunny", "redmond", "cupertino", "blitzer", "lightness", "humanity",
51
            "eggplant", "excitebyke", "flick", "images", "peppergrinder", "overcast", "lefrog", "southstreet", "start");
52
53 1
        $mwbs = array();
54
55 1
        if ($fs->exists($docDir)) {
56 1
            $finder->in($docDir)->files()->name('*.mwb');
57 1
            foreach ($finder as $file) {
58 1
                $mwbs[] = $file->getBasename();
59
            }
60
        }
61
62 1
        if ($fs->exists($projectDir . '/.svn')) {
63
            $svn = true;
64
        } else {
65 1
            $svn = false;
66
        }
67
68 1
        if ($fs->exists($projectDir . '/.git')) {
69
            $git = true;
70
        } else {
71 1
            $git = false;
72
        }
73
74 1
        if (!OsFunctions::isWindows()) {
75 1
            $delcmd = 'rm -rf';
76 1
            $setfilelock = "touch " . $this->getParameter("maintenanceLockFilePath");
77 1
            $remfilelock = "rm " . $this->getParameter("maintenanceLockFilePath");
78 1
            $windows = false;
79
        } else {
80
            $delcmd = 'del';
81
            $setfilelock = 'echo $null >> ' . $this->getParameter("maintenanceLockFilePath");
82
            $remfilelock = "del " . $this->getParameter("maintenanceLockFilePath");
83
            $windows = true;
84
        }
85
86 1
        $dellockfile = "DELETELOCK";
87 1
        $delcomposerfile = $delcmd . ' ' . $projectDir . DIRECTORY_SEPARATOR . 'composer.lock';
88 1
        $dellogsfiles = $delcmd . ' ' . $this->apppaths->getLogsPath() . DIRECTORY_SEPARATOR . '*';
89 1
        $delcachefiles = $delcmd . ' ' . $this->apppaths->getCachePath() . DIRECTORY_SEPARATOR . '*';
90 1
        $setmaintenancefile = $setfilelock;
91 1
        $remmaintenancefile = $remfilelock;
92
93
        $comandishell = array(
94 1
            'lockfile' => $dellockfile,
95 1
            'composerlock' => $this->fixSlash($delcomposerfile),
96 1
            'logsfiles' => $this->fixSlash($dellogsfiles),
97 1
            'cachefiles' => $this->fixSlash($delcachefiles),
98 1
            'setmaintenancefile' => $setmaintenancefile,
99 1
            'remmaintenancefile' => $remmaintenancefile,
100
        );
101
102 1
        $twigparms = array('svn' => $svn, 'git' => $git, 'bundles' => $bundles, 'mwbs' => $mwbs,
103 1
            'rootdir' => $this->fixSlash($projectDir),
104 1
            'comandishell' => $comandishell, 'iswindows' => $windows,
105 1
            'themes' => $themes);
106
107 1
        return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:index.html.twig', $twigparms);
108
    }
109
110 1
    private function fixSlash($path)
111
    {
112 1
        return str_replace('\\', '\\\\', $path);
113
    }
114
115
    private function getLockMessage()
116
    {
117
        return "<h2 style='color: orange;'>E' già in esecuzione un comando, riprova tra qualche secondo!</h2>";
118
    }
119
120 1
    public function aggiornaSchemaDatabaseAction()
121
    {
122 1
        if (!$this->locksystem->acquire()) {
123
            return new Response($this->getLockMessage());
124
        } else {
125 1
            $this->locksystem->acquire();
126 1
            $command = $this->get("pannelloamministrazione.commands");
127 1
            $result = $command->aggiornaSchemaDatabase();
128
129 1
            $this->locksystem->release();
130 1
            $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
131
132 1
            return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
133
        }
134
    }
135
136
    /* FORMS */
137
138 1
    public function generateFormCrudAction(Request $request)
139
    {
140 1
        if (!$this->locksystem->acquire()) {
141
            return new Response($this->getLockMessage());
142
        } else {
143 1
            $entityform = $request->get('entityform');
144
145 1
            $this->locksystem->acquire();
146
147 1
            $command = $this->get("pannelloamministrazione.commands");
148 1
            $ret = $command->generateFormCrud($entityform);
149
150 1
            $this->locksystem->release();
151
            //$retcc = '';
152 1
            if ($ret['errcode'] < 0) {
153
                return new Response($ret['message']);
154
            } else {
155
                //$retcc = $command->clearCacheEnv($this->get('kernel')->getEnvironment());
156
            }
157 1
            $twigparms = array('errcode' => $ret['errcode'], 'command' => $ret['command'], 'message' => $ret['message']);
158
159 1
            return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
160
        }
161
    }
162
163
    /* ENTITIES */
164
165 1
    public function generateEntityAction(Request $request)
166
    {
167 1
        if (!$this->locksystem->acquire()) {
168
            return new Response($this->getLockMessage());
169
        } else {
170 1
            $this->locksystem->acquire();
171 1
            $wbFile = $request->get('file');
172 1
            $command = $this->get("pannelloamministrazione.commands");
173 1
            $ret = $command->generateEntity($wbFile);
174 1
            $this->locksystem->release();
175 1
            return new Response($ret['message']);
176
        }
177
    }
178
179
    /* VCS (GIT,SVN) */
180
181
    /**
182
     * @codeCoverageIgnore
183
     */
184
    public function getVcsAction()
185
    {
186
        set_time_limit(0);
187
        $this->apppaths = $this->get("pannelloamministrazione.projectpath");
188
        if (!$this->locksystem->acquire()) {
189
            return new Response($this->getLockMessage());
190
        } else {
191
            $this->locksystem->acquire();
192
            $command = $this->get("pannelloamministrazione.commands");
193
            $result = $command->getVcs();
194
            $this->locksystem->release();
195
            if ($result['errcode'] < 0) {
196
                $responseout = '<pre>Errore nel comando: <i style = "color: white;">' . $result['command'] . '</i>'
197
                        . '<br/><i style = "color: red;">' . nl2br($result['errmsg']) . '</i></pre>';
198
            } else {
199
                $responseout = '<pre>Eseguito comando: <i style = "color: white;">' . $result['command'] . '</i><br/>' .
200
                        nl2br($result['errmsg']) . '</pre>';
201
            }
202
203
            return new Response($responseout);
204
        }
205
    }
206
207
    /* CLEAR CACHE */
208
209
    /**
210
     * Suppress PMD warnings per exit.
211
     *
212
     * @//SuppressWarnings(PHPMD)
213
     */
214 1
    public function clearCacheAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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

214
    public function clearCacheAction(/** @scrutinizer ignore-unused */ Request $request)

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...
215
    {
216 1
        set_time_limit(0);
217 1
        if (!$this->locksystem->acquire()) {
218
            return new Response($this->getLockMessage());
219
        } else {
220 1
            $this->locksystem->acquire();
221 1
            $command = $this->get("pannelloamministrazione.commands");
222 1
            $result = $command->clearcache();
223
224 1
            $this->locksystem->release();
225
226
            /* Uso exit perchè new response avendo cancellato la cache schianta non avendo più a disposizione i file */
227
            //return $commanddev . '<br/>' . $cmdoutputdev . '<br/><br/>' . $commandprod . '<br/>' . $cmdoutputprod;
228 1
            return new Response(nl2br($result));
229
            //exit(nl2br($result));
230
            //return dump($result);
231
        }
232
    }
233
234
    /* CLEAR CACHE */
235
236 1
    public function symfonyCommandAction(Request $request)
237
    {
238 1
        set_time_limit(0);
239 1
        $comando = $request->get('symfonycommand');
240 1
        if (!$this->locksystem->acquire()) {
241
            return new Response($this->getLockMessage());
242
        } else {
243 1
            $this->locksystem->acquire();
244 1
            $this->apppaths = $this->get("pannelloamministrazione.projectpath");
245 1
            $pammutils = new PannelloAmministrazioneUtils($this->container);
246 1
            $phpPath = OsFunctions::getPHPExecutableFromPath();
247 1
            $result = $pammutils->runCommand($phpPath . ' ' . $this->apppaths->getConsole() . ' ' . $comando);
248
249 1
            $this->locksystem->release();
250 1
            if ($result['errcode'] < 0) {
251
                $responseout = 'Errore nel comando: <i style = "color: white;">' .
252
                        str_replace(';', '<br/>', str_replace('&&', '<br/>', $comando)) .
253
                        '</i><br/><i style = "color: red;">' . nl2br($result['errmsg']) . '</i>';
254
255
                return new Response($responseout);
256
            }
257
            $responseout = '<pre>Eseguito comando:<br/><br/><i style = "color: white;">' .
258 1
                    str_replace(';', '<br/>', str_replace('&&', '<br/>', $comando)) . '</i><br/><br/>' .
259 1
                    str_replace("\n", '<br/>', $result['errmsg']) . '</pre>';
260
261 1
            return new Response($responseout);
262
        }
263
    }
264
265
    /**
266
     * Suppress PMD warnings per exit.
267
     *
268
     * @SuppressWarnings(PHPMD)
269
     */
270 1
    public function unixCommandAction(Request $request)
271
    {
272 1
        set_time_limit(0);
273 1
        $pammutils = new PannelloAmministrazioneUtils($this->container);
274 1
        $command = $request->get('unixcommand');
275
        //Se viene lanciato il comando per cancellare il file di lock su bypassa tutto e si lancia
276 1
        $dellockfile = "DELETELOCK";
277 1
        if ($command == $dellockfile) {
278
            $this->locksystem->release();
279
            return new Response('File di lock cancellato');
280
        }
281
282 1
        if (!$this->locksystem->acquire()) {
283
            return new Response($this->getLockMessage());
284
        } else {
285 1
            $this->locksystem->acquire();
286 1
            $result = $pammutils->runCommand($command);
287
288 1
            $this->locksystem->release();
289
            // eseguito deopo la fine del comando
290 1
            if ($result['errcode'] < 0) {
291
                $errmsg = 'Errore nel comando: <i style = "color: white;">' .
292
                        str_replace(';', '<br/>', str_replace('&&', '<br/>', $command)) .
293
                        '</i><br/><i style = "color: red;">' . nl2br($result['errmsg']) . '</i>';
294
295
                return new Response($errmsg);
296
                //exit(nl2br($errmsg));
297
                //Uso exit perchè new response avendo cancellato la cache schianta non avendo più a disposizione i file
298
                //return;
299
                /* return new Response('Errore nel comando: <i style = "color: white;">' .
300
                 * $command . '</i><br/><i style = "color: red;">' . str_replace("\n", '<br/>', $process->getErrorOutput()) . '</i>'); */
301
            }
302
            $msgok = '<pre>Eseguito comando:<br/><i style = "color: white;"><br/>' .
303 1
                    str_replace(';', '<br/>', str_replace('&&', '<br/>', $command)) . '</i><br/>' .
304 1
                    nl2br($result['errmsg']) . '</pre>';
305
            //Uso exit perchè new response avendo cancellato la cache schianta non avendo più a disposizione i file
306 1
            return new Response($msgok);
307
            //exit(nl2br($msgok));
308
            //return;
309
            /* return new Response('<pre>Eseguito comando: <i style = "color: white;">' . $command .
310
             * '</i><br/>' . str_replace("\n", "<br/>", $process->getOutput()) . "</pre>"); */
311
        }
312
    }
313
314
    /**
315
     * @codeCoverageIgnore
316
     */
317
    public function phpunittestAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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

317
    public function phpunittestAction(/** @scrutinizer ignore-unused */ Request $request)

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...
318
    {
319
        set_time_limit(0);
320
        $this->apppaths = $this->get("pannelloamministrazione.projectpath");
321
        if (!$this->locksystem->acquire()) {
322
            return new Response($this->getLockMessage());
323
        } else {
324
            if (!OsFunctions::isWindows()) {
325
                $this->locksystem->acquire();
326
                //$phpPath = OsFunctions::getPHPExecutableFromPath();
327
                $sepchr = OsFunctions::getSeparator();
328
                $phpPath = OsFunctions::getPHPExecutableFromPath();
329
330
                $command = 'cd ' . $this->apppaths->getRootPath() . $sepchr .
331
                        $phpPath . ' ' . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'phpunit';
332
333
                $process = new Process($command);
334
                $process->run();
335
336
                $this->locksystem->release();
337
                // eseguito deopo la fine del comando
338
                /* if (!$process->isSuccessful()) {
339
                  return new Response('Errore nel comando: <i style = "color: white;">' .
340
                 * $command . '</i><br/><i style = "color: red;">' . str_replace("\n",
341
                 * '<br/>', $process->getErrorOutput()) . '</i>');
342
                  } */
343
                $responseout = '<pre>Eseguito comando: <i style = "color: white;">' . $command . '</i><br/>' .
344
                        str_replace("\n", '<br/>', $process->getOutput()) . '</pre>';
345
346
                return new Response($responseout);
347
            } else {
348
                return new Response('Non previsto in ambiente windows!');
349
            }
350
        }
351
    }
352
353 1
    public function changethemeAction(Request $request)
354
    {
355 1
        set_time_limit(0);
356 1
        $this->apppaths = $this->get("pannelloamministrazione.projectpath");
357 1
        $temascelto = $request->get("theme");
358 1
        $envfile = $this->apppaths->getRootPath() . '/.env';
359 1
        if (!$this->locksystem->acquire()) {
360
            return new Response($this->getLockMessage());
361
        } else {
362 1
            $this->locksystem->acquire();
363 1
            $replacedenv = preg_replace('~^temascelto=.*$~m', "temascelto=" . $temascelto, file_get_contents($envfile));
364 1
            file_put_contents($envfile, $replacedenv);
365
366 1
            $this->locksystem->release();
367 1
            $responseout = '<pre>Thema selezionato: <i style = "color: white;">' . $temascelto . '</i>' .
368 1
                    '<br/>Aggiorna la pagina per renderlo effettivo<br/></pre>';
369
370 1
            return new Response($responseout);
371
        }
372
    }
373
}
374