Passed
Push — develop ( 11585d...bd8de4 )
by Andrea
35:10
created

aggiornaSchemaDatabase()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.9256

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 19
nc 9
nop 0
dl 0
loc 26
ccs 12
cts 18
cp 0.6667
crap 5.9256
rs 9.3222
c 1
b 1
f 0
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 1
    private function fixSlash($path)
136
    {
137 1
        return str_replace('\\', '\\\\', $path);
138
    }
139
    private function getLockMessage()
140
    {
141
        return "<h2 style='color: orange;'>E' già in esecuzione un comando, riprova tra qualche secondo!</h2>";
142
    }
143 1
    public function aggiornaSchemaDatabase()
144
    {
145 1
        if (!$this->locksystem->acquire()) {
146
            return new Response($this->getLockMessage());
147
        } else {
148 1
            $this->locksystem->acquire();
149 1
            $driver = $this->getDoctrine()->getManager()->getConnection()->getDatabasePlatform()->getName();
150 1
            if ($driver == 'sqlite') {
151
                $this->getDoctrine()->getManager()->getConnection()->exec('PRAGMA foreign_keys = OFF;');
152
            }
153 1
            $command = $this->pacommands;
154 1
            $result = $command->aggiornaSchemaDatabase();
155 1
            if ($driver == 'sqlite') {
156
                $this->getDoctrine()->getManager()->getConnection()->exec('PRAGMA foreign_keys = ON;');
157
            }
158
159 1
            $this->locksystem->release();
160 1
            if (0 != $result['errcode']) {
161
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
162
                $view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
163
164
                return new Response($view, 500);
165
            } else {
166 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
167
168 1
                return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
169
            }
170
        }
171
    }
172
    /* FORMS */
173 1
    public function generateFormCrud(Request $request)
174
    {
175 1
        if (!$this->locksystem->acquire()) {
176
            return new Response($this->getLockMessage());
177
        } else {
178 1
            $entityform = $request->get('entityform');
179 1
            $generatemplate = 'true' === $request->get('generatemplate') ? true : false;
180 1
            $this->locksystem->acquire();
181
182 1
            $command = $this->pacommands;
183 1
            $result = $command->generateFormCrud($entityform, $generatemplate);
184
185 1
            $this->locksystem->release();
186
            //$retcc = '';
187 1
            if ($result['errcode'] < 0) {
188
                $twigparms = array('errcode' => $result['errcode'], 'command' => 'Generazione Form Crud', 'message' => $result['message']);
189
190
                return new Response(
191
                    $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms),
192
                    500
193
                );
194
            } else {
195
                //$retcc = $command->clearCacheEnv($this->get('kernel')->getEnvironment());
196
            }
197 1
            $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
198
199 1
            return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
200
        }
201
    }
202
    /* ENTITIES */
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
    /* VCS (GIT,SVN) */
227
    /**
228
     * @codeCoverageIgnore
229
     */
230
    public function getVcs()
231
    {
232
        set_time_limit(0);
233
        $this->apppaths = $this->apppaths;
234
        if (!$this->locksystem->acquire()) {
235
            return new Response($this->getLockMessage());
236
        } else {
237
            $this->locksystem->acquire();
238
            $command = $this->pacommands;
239
            $result = $command->getVcs();
240
            $this->locksystem->release();
241
            $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
242
243
            return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms);
244
        }
245
    }
246
    /* CLEAR CACHE */
247
    /**
248
     * Suppress PMD warnings per exit.
249
     *
250
     * @//SuppressWarnings(PHPMD)
251
     */
252
    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

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

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