Passed
Push — develop ( bd8de4...109f3e )
by Andrea
03:28
created

PannelloAmministrazioneController   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 366
Duplicated Lines 0 %

Test Coverage

Coverage 81.71%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
eloc 216
c 7
b 2
f 0
dl 0
loc 366
ccs 134
cts 164
cp 0.8171
rs 9.36
wmc 38

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getVcs() 0 14 2
A aggiornaSchemaDatabase() 0 19 3
A clearCache() 0 21 3
A generateEntity() 0 20 3
A unixCommand() 0 30 4
A generateFormCrud() 0 27 4
A symfonyCommand() 0 24 3
A __construct() 0 11 1
A phpunittest() 0 31 4
B index() 0 76 5
A findEntities() 0 15 4
A fixSlash() 0 3 1
A getLockMessage() 0 3 1
1
<?php
2
3
namespace Cdf\PannelloAmministrazioneBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
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 Symfony\Component\Lock\Factory;
13
use Symfony\Component\Lock\Store\FlockStore;
14
use Cdf\PannelloAmministrazioneBundle\Utils\Utility as Pautils;
15
use Cdf\PannelloAmministrazioneBundle\Utils\ProjectPath;
16
use Cdf\PannelloAmministrazioneBundle\Utils\Commands as Pacmd;
17
18
class PannelloAmministrazioneController extends AbstractController
19
{
20
    private $apppaths;
21
    private $pacommands;
22
    private $pautils;
23
    protected $locksystem;
24
    protected $factory;
25
    private $appname;
26
    private $lockfile;
27
28 1
    public function __construct($appname, $lockfile, ProjectPath $projectpath, Pacmd $pacommands, Pautils $pautils)
29
    {
30 1
        $store = new FlockStore(sys_get_temp_dir());
31 1
        $factory = new Factory($store);
32 1
        $this->locksystem = $factory->createLock('pannelloamministrazione-command');
33 1
        $this->locksystem->release();
34 1
        $this->appname = $appname;
35 1
        $this->lockfile = $lockfile;
36 1
        $this->apppaths = $projectpath;
37 1
        $this->pacommands = $pacommands;
38 1
        $this->pautils = $pautils;
39 1
    }
40
41 1
    private function findEntities()
42
    {
43 1
        $entitiesprogetto = array();
44 1
        $prefix = 'App\\Entity\\';
45 1
        $prefixBase = 'Base';
46 1
        $entities = $this->get('doctrine')->getManager()->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();
47 1
        foreach ($entities as $entity) {
48 1
            if (substr($entity, 0, strlen($prefix)) == $prefix) {
49 1
                if (substr(substr($entity, strlen($prefix)), 0, strlen($prefixBase)) != $prefixBase) {
50 1
                    $entitiesprogetto[] = substr($entity, strlen($prefix));
51
                }
52
            }
53
        }
54
55 1
        return $entitiesprogetto;
56
    }
57
58 1
    public function index()
59
    {
60 1
        $finder = new Finder();
61 1
        $fs = new Filesystem();
62
63 1
        $projectDir = $this->apppaths->getRootPath();
64 1
        $docDir = $this->apppaths->getDocPath();
65
66 1
        $mwbs = array();
67
68 1
        if ($fs->exists($docDir)) {
69 1
            $finder->in($docDir)->files()->name('*.mwb');
70 1
            foreach ($finder as $file) {
71 1
                $mwbs[] = $file->getBasename();
72
            }
73
        }
74 1
        sort($mwbs);
75 1
        $svn = $fs->exists($projectDir.'/.svn');
76 1
        $git = $fs->exists($projectDir.'/.git');
77 1
        if (!OsFunctions::isWindows()) {
78 1
            $delcmd = 'rm -rf';
79 1
            $setfilelock = 'touch '.$this->lockfile;
80 1
            $remfilelock = 'rm '.$this->lockfile;
81 1
            $windows = false;
82
        } else {
83
            // @codeCoverageIgnoreStart
84
            $delcmd = 'del';
85
            $setfilelock = 'echo $null >> '.$this->lockfile;
86
            $remfilelock = 'del '.$this->lockfile;
87
            $windows = true;
88
            // @codeCoverageIgnoreEnd
89
        }
90 1
        $dellogsfiles = $delcmd.' '.$this->apppaths->getLogsPath().DIRECTORY_SEPARATOR.'*';
91 1
        $delcacheprodfiles = $delcmd.' '.$this->apppaths->getCachePath().DIRECTORY_SEPARATOR.'prod'.DIRECTORY_SEPARATOR.'*';
92 1
        $delcachedevfiles = $delcmd.' '.$this->apppaths->getCachePath().DIRECTORY_SEPARATOR.'dev'.DIRECTORY_SEPARATOR.'*';
93 1
        $setmaintenancefile = $setfilelock;
94 1
        $remmaintenancefile = $remfilelock;
95
96
        $comandishell = array(
97 1
            array('text' => $this->fixSlash($dellogsfiles), 'link' => '#'),
98 1
            array('text' => $this->fixSlash($delcacheprodfiles), 'link' => '#'),
99 1
            array('text' => $this->fixSlash($delcachedevfiles), 'link' => '#'),
100 1
            array('text' => $this->fixSlash($setmaintenancefile), 'link' => '#'),
101 1
            array('text' => $this->fixSlash($remmaintenancefile), 'link' => '#'),
102
                //array("text"=>"prova", "link"=>"#"),
103
        );
104 1
        $composerinstall = '';
105 1
        if (false == $windows) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
106 1
            $composerinstall = $composerinstall.' cd '.$projectDir.' && composer install --no-interaction 2>&1';
107 1
            $sed = "sed -i -e 's/cercaquestastringa/sostituisciconquestastringa/g' ".$projectDir.'/.env';
108 1
            $comandishell[] = array('text' => $composerinstall, 'link' => '#');
109 1
            $comandishell[] = array('text' => $sed, 'link' => '#');
110
        }
111
112
        $comandisymfony = array(
113 1
            array('text' => 'list', 'link' => '#'),
114
            array('text' => 'cache:clear --env=prod --no-debug', 'link' => '#'),
115
            array('text' => 'fos:user:create admin pass [email protected]', 'link' => '#'),
116
            array('text' => 'fos:user:promote username ROLE_SUPER_ADMIN', 'link' => '#'),
117 1
            array('text' => "assets:install $projectDir/public", 'link' => '#'),
118
            array('text' => 'pannelloamministrazione:checkgitversion', 'link' => '#'),
119
        );
120
121 1
        $entities = $this->findEntities();
122 1
        sort($entities);
123
124
        $twigparms = array(
125 1
            'svn' => $svn, 'git' => $git, 'mwbs' => $mwbs, 'entities' => $entities,
126 1
            'rootdir' => $this->fixSlash($projectDir),
127 1
            'comandishell' => $comandishell,
128 1
            'comandisymfony' => $comandisymfony,
129 1
            'iswindows' => $windows,
130 1
            'appname' => $this->appname,
131
        );
132
133 1
        return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:index.html.twig', $twigparms);
134
    }
135
136 1
    private function fixSlash($path)
137
    {
138 1
        return str_replace('\\', '\\\\', $path);
139
    }
140
141
    private function getLockMessage()
142
    {
143
        return "<h2 style='color: orange;'>E' già in esecuzione un comando, riprova tra qualche secondo!</h2>";
144
    }
145
146 1
    public function aggiornaSchemaDatabase()
147
    {
148 1
        if (!$this->locksystem->acquire()) {
149
            return new Response($this->getLockMessage());
150
        } else {
151 1
            $this->locksystem->acquire();
152 1
            $command = $this->pacommands;
153 1
            $result = $command->aggiornaSchemaDatabase();
154
155 1
            $this->locksystem->release();
156 1
            if (0 != $result['errcode']) {
157
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
158
                $view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
159
160
                return new Response($view, 500);
161
            } else {
162 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
163
164 1
                return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
165
            }
166
        }
167
    }
168
169
    /* FORMS */
170
171 1
    public function generateFormCrud(Request $request)
172
    {
173 1
        if (!$this->locksystem->acquire()) {
174
            return new Response($this->getLockMessage());
175
        } else {
176 1
            $entityform = $request->get('entityform');
177 1
            $generatemplate = 'true' === $request->get('generatemplate') ? true : false;
178 1
            $this->locksystem->acquire();
179
180 1
            $command = $this->pacommands;
181 1
            $result = $command->generateFormCrud($entityform, $generatemplate);
182
183 1
            $this->locksystem->release();
184
            //$retcc = '';
185 1
            if ($result['errcode'] < 0) {
186
                $twigparms = array('errcode' => $result['errcode'], 'command' => 'Generazione Form Crud', 'message' => $result['message']);
187
188
                return new Response(
189
                    $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms),
190
                    500
191
                );
192
            } else {
193
                //$retcc = $command->clearCacheEnv($this->get('kernel')->getEnvironment());
194
            }
195 1
            $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
196
197 1
            return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
198
        }
199
    }
200
201
    /* ENTITIES */
202
203 1
    public function generateEntity(Request $request)
204
    {
205 1
        if (!$this->locksystem->acquire()) {
206
            return new Response($this->getLockMessage());
207
        } else {
208 1
            $this->locksystem->acquire();
209 1
            $wbFile = $request->get('file');
210 1
            $command = $this->pacommands;
211 1
            $result = $command->generateEntity($wbFile);
212 1
            $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
0 ignored issues
show
Unused Code introduced by
The assignment to $twigparms is dead and can be removed.
Loading history...
213 1
            $this->locksystem->release();
214 1
            if (0 != $result['errcode']) {
215 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
216 1
                $view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
217
218 1
                return new Response($view, 500);
219
            } else {
220 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
221
222 1
                return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
223
            }
224
        }
225
    }
226
227
    /* VCS (GIT,SVN) */
228
229
    /**
230
     * @codeCoverageIgnore
231
     */
232
    public function getVcs()
233
    {
234
        set_time_limit(0);
235
        $this->apppaths = $this->apppaths;
236
        if (!$this->locksystem->acquire()) {
237
            return new Response($this->getLockMessage());
238
        } else {
239
            $this->locksystem->acquire();
240
            $command = $this->pacommands;
241
            $result = $command->getVcs();
242
            $this->locksystem->release();
243
            $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
244
245
            return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
246
        }
247
    }
248
249
    /* CLEAR CACHE */
250
251
    /**
252
     * Suppress PMD warnings per exit.
253
     *
254
     * @//SuppressWarnings(PHPMD)
255
     */
256
    public function clearCache(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

256
    public function clearCache(/** @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...
257
    {
258
        set_time_limit(0);
259
        if (!$this->locksystem->acquire()) {
260
            return new Response($this->getLockMessage());
261
        } else {
262
            $this->locksystem->acquire();
263
            $command = $this->pacommands;
264
            $result = $command->clearcache();
265
266
            $this->locksystem->release();
267
268
            if (0 != $result['errcode']) {
269
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
270
                $view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
271
272
                return new Response($view, 500);
273
            } else {
274
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
275
276
                return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
277
            }
278
        }
279
    }
280
281
    /* CLEAR CACHE */
282
283 1
    public function symfonyCommand(Request $request)
284
    {
285 1
        set_time_limit(0);
286
287 1
        $simfonycommand = $request->get('symfonycommand');
288 1
        if (!$this->locksystem->acquire()) {
289
            return new Response($this->getLockMessage());
290
        } else {
291 1
            $this->locksystem->acquire();
292 1
            $this->apppaths = $this->apppaths;
293 1
            $pammutils = $this->pautils;
294 1
            $command = $this->apppaths->getConsole().' '.$simfonycommand;
295 1
            $result = $pammutils->runCommand($command);
296
297 1
            $this->locksystem->release();
298 1
            if (0 != $result['errcode']) {
299 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
300 1
                $view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
301
302 1
                return new Response($view, 500);
303
            } else {
304 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
305
306 1
                return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
307
            }
308
        }
309
    }
310
311
    /**
312
     * Suppress PMD warnings per exit.
313
     *
314
     * @SuppressWarnings(PHPMD)
315
     */
316 1
    public function unixCommand(Request $request)
317
    {
318 1
        set_time_limit(0);
319 1
        $pammutils = $this->pautils;
320 1
        $unixcommand = $request->get('unixcommand');
321
        //Se viene lanciato il comando per cancellare il file di lock su bypassa tutto e si lancia
322 1
        $dellockfile = 'DELETELOCK';
323 1
        if ($unixcommand == $dellockfile) {
324
            $this->locksystem->release();
325
326
            return new Response('File di lock cancellato');
327
        }
328
329 1
        if (!$this->locksystem->acquire()) {
330
            return new Response($this->getLockMessage());
331
        } else {
332 1
            $this->locksystem->acquire();
333 1
            $result = $pammutils->runCommand($unixcommand);
334
335 1
            $this->locksystem->release();
336
            // eseguito deopo la fine del comando
337 1
            if (0 != $result['errcode']) {
338 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
339 1
                $view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
340
341 1
                return new Response($view, 500);
342
            } else {
343 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
344
345 1
                return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
346
            }
347
        }
348
    }
349
350
    /**
351
     * @codeCoverageIgnore
352
     */
353
    public function phpunittest(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

353
    public function phpunittest(/** @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...
354
    {
355
        set_time_limit(0);
356
        $this->apppaths = $this->apppaths;
357
        if (!$this->locksystem->acquire()) {
358
            return new Response($this->getLockMessage());
359
        } else {
360
            if (!OsFunctions::isWindows()) {
361
                $this->locksystem->acquire();
362
                //$phpPath = OsFunctions::getPHPExecutableFromPath();
363
                $command = 'vendor'.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'simple-phpunit';
364
                $process = new Process(array($command));
365
                $process->setWorkingDirectory($this->apppaths->getRootPath());
366
367
                $process->run();
368
369
                $this->locksystem->release();
370
                // eseguito dopo la fine del comando
371
                if (!$process->isSuccessful()) {
372
                    $twigparms = array('errcode' => -1, 'command' => $command, 'message' => $process->getOutput().$process->getErrorOutput());
373
                    $view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
374
375
                    return new Response($view, 500);
376
                } else {
377
                    $twigparms = array('errcode' => 0, 'command' => $command, 'message' => $process->getOutput().$process->getErrorOutput());
378
379
                    return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
380
                }
381
            } else {
382
                // @codeCoverageIgnoreStart
383
                return new Response('Non previsto in ambiente windows!', 500);
384
                // @codeCoverageIgnoreEnd
385
            }
386
        }
387
    }
388
}
389