Passed
Push — master ( a74354...254a60 )
by Andrea
18:29 queued 11s
created

PannelloAmministrazioneController::findAPIModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 2
b 0
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\LockFactory;
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
use Cdf\BiCoreBundle\Utils\Api\ApiUtils;
18
19
class PannelloAmministrazioneController extends AbstractController
20
{
21
    private $apppaths;
22
    private $pacommands;
23
    private $pautils;
24
    protected $locksystem;
25
    protected $factory;
26
    private $appname;
27
    private $lockfile;
28
29 1
    public function __construct($appname, $lockfile, ProjectPath $projectpath, Pacmd $pacommands, Pautils $pautils)
30
    {
31 1
        $store = new FlockStore(sys_get_temp_dir());
32 1
        $factory = new LockFactory($store);
33 1
        $this->locksystem = $factory->createLock('pannelloamministrazione-command');
34 1
        $this->locksystem->release();
35 1
        $this->appname = $appname;
36 1
        $this->lockfile = $lockfile;
37 1
        $this->apppaths = $projectpath;
38 1
        $this->pacommands = $pacommands;
39 1
        $this->pautils = $pautils;
40 1
    }
41
42 1
    private function findEntities()
43
    {
44 1
        $entitiesprogetto = array();
45 1
        $prefix = 'App\\Entity\\';
46 1
        $prefixBase = 'Base';
47 1
        $entities = $this->get('doctrine')->getManager()->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();
48
        // compute additional API models since external bundles
49 1
        $additionalEntities = $this->findAPIModels();
50 1
        foreach ($entities as $entity) {
51 1
            if (substr($entity, 0, strlen($prefix)) == $prefix) {
52 1
                if (substr(substr($entity, strlen($prefix)), 0, strlen($prefixBase)) != $prefixBase) {
53 1
                    $entitiesprogetto[] = substr($entity, strlen($prefix));
54
                }
55
            }
56
        }
57
        // merge of found arrays
58 1
        $outcomes = array_merge($entitiesprogetto, $additionalEntities);
59 1
        return $outcomes;
60
    }
61
62
    /**
63
     * It looks for Models existent into included external bundles.
64
     * It uses ApiUtils in order to know where to search and what look for.
65
     */
66 1
    private function findAPIModels(): array
67
    {
68 1
        return ApiUtils::apiModels();
0 ignored issues
show
Bug Best Practice introduced by
The method Cdf\BiCoreBundle\Utils\Api\ApiUtils::apiModels() is not static, but was called statically. ( Ignorable by Annotation )

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

68
        return ApiUtils::/** @scrutinizer ignore-call */ apiModels();
Loading history...
69
    }
70
71 1
    public function index()
72
    {
73 1
        $finder = new Finder();
74 1
        $fs = new Filesystem();
75
76 1
        $projectDir = $this->apppaths->getRootPath();
77 1
        $docDir = $this->apppaths->getDocPath();
78
79 1
        $mwbs = array();
80
81 1
        if ($fs->exists($docDir)) {
82 1
            $finder->in($docDir)->files()->name('*.mwb');
83 1
            foreach ($finder as $file) {
84 1
                $mwbs[] = $file->getBasename();
85
            }
86
        }
87 1
        sort($mwbs);
88 1
        $svn = $fs->exists($projectDir.'/.svn');
89 1
        $git = $fs->exists($projectDir.'/.git');
90 1
        if (!OsFunctions::isWindows()) {
91 1
            $delcmd = 'rm -rf';
92 1
            $setfilelock = 'touch '.$this->lockfile;
93 1
            $remfilelock = 'rm '.$this->lockfile;
94 1
            $windows = false;
95
        } else {
96
            // @codeCoverageIgnoreStart
97
            $delcmd = 'del';
98
            $setfilelock = 'echo $null >> '.$this->lockfile;
99
            $remfilelock = 'del '.$this->lockfile;
100
            $windows = true;
101
            // @codeCoverageIgnoreEnd
102
        }
103 1
        $dellogsfiles = $delcmd.' '.$this->apppaths->getLogsPath().DIRECTORY_SEPARATOR.'*';
104 1
        $delcacheprodfiles = $delcmd.' '.$this->apppaths->getCachePath().DIRECTORY_SEPARATOR.'prod'.DIRECTORY_SEPARATOR.'*';
105 1
        $delcachedevfiles = $delcmd.' '.$this->apppaths->getCachePath().DIRECTORY_SEPARATOR.'dev'.DIRECTORY_SEPARATOR.'*';
106 1
        $setmaintenancefile = $setfilelock;
107 1
        $remmaintenancefile = $remfilelock;
108
109
        $comandishell = array(
110 1
            array('text' => $this->fixSlash($dellogsfiles), 'link' => '#'),
111 1
            array('text' => $this->fixSlash($delcacheprodfiles), 'link' => '#'),
112 1
            array('text' => $this->fixSlash($delcachedevfiles), 'link' => '#'),
113 1
            array('text' => $this->fixSlash($setmaintenancefile), 'link' => '#'),
114 1
            array('text' => $this->fixSlash($remmaintenancefile), 'link' => '#'),
115
                //array("text"=>"prova", "link"=>"#"),
116
        );
117 1
        $composerinstall = '';
118 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...
119 1
            $composerinstall = $composerinstall.' cd '.$projectDir.' && composer install --no-dev --optimize-autoloader --no-interaction 2>&1';
120 1
            $sed = "sed -i -e 's/cercaquestastringa/sostituisciconquestastringa/g' ".$projectDir.'/.env';
121 1
            $comandishell[] = array('text' => $composerinstall, 'link' => '#');
122 1
            $comandishell[] = array('text' => $sed, 'link' => '#');
123
        }
124
125
        $comandisymfony = array(
126 1
            array('text' => 'list', 'link' => '#'),
127
            array('text' => 'cache:clear --env=prod --no-debug', 'link' => '#'),
128
            array('text' => 'fos:user:create admin pass [email protected]', 'link' => '#'),
129
            array('text' => 'fos:user:promote username ROLE_SUPER_ADMIN', 'link' => '#'),
130 1
            array('text' => "assets:install $projectDir/public", 'link' => '#'),
131
            array('text' => 'pannelloamministrazione:checkgitversion', 'link' => '#'),
132
        );
133
134 1
        $entities = $this->findEntities();
135 1
        sort($entities);
136
137
        $twigparms = array(
138 1
            'svn' => $svn, 'git' => $git, 'mwbs' => $mwbs, 'entities' => $entities,
139 1
            'rootdir' => $this->fixSlash($projectDir),
140 1
            'comandishell' => $comandishell,
141 1
            'comandisymfony' => $comandisymfony,
142 1
            'iswindows' => $windows,
143 1
            'appname' => $this->appname,
144
        );
145
146 1
        return $this->render('@PannelloAmministrazione/PannelloAmministrazione/index.html.twig', $twigparms);
147
    }
148
149 1
    private function fixSlash($path)
150
    {
151 1
        return str_replace('\\', '\\\\', $path);
152
    }
153
154
    private function getLockMessage()
155
    {
156
        return "<h2 style='color: orange;'>E' già in esecuzione un comando, riprova tra qualche secondo!</h2>";
157
    }
158
159 1
    public function aggiornaSchemaDatabase()
160
    {
161 1
        if (!$this->locksystem->acquire()) {
162
            return new Response($this->getLockMessage());
163
        } else {
164 1
            $this->locksystem->acquire();
165 1
            $command = $this->pacommands;
166 1
            $result = $command->aggiornaSchemaDatabase();
167
168 1
            $this->locksystem->release();
169 1
            if (0 != $result['errcode']) {
170
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
171
                $view = $this->renderView('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
172
173
                return new Response($view, 500);
174
            } else {
175 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
176
177 1
                return $this->render('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
178
            }
179
        }
180
    }
181
182
    /* FORMS */
183
184
    /**
185
     * Generate form item, controllers and twigs for the given entity/model
186
     */
187 1
    public function generateFormCrud(Request $request)
188
    {
189 1
        if (!$this->locksystem->acquire()) {
190
            return new Response($this->getLockMessage());
191
        } else {
192 1
            $entityform = $request->get('entityform');
193 1
            $generatemplate = 'true' === $request->get('generatemplate') ? true : false;
194 1
            $this->locksystem->acquire();
195
196
            //we are working with an API?
197 1
            $isApi = false;
198
            //verify if provided string belongs to an API model
199 1
            if (\str_contains($entityform, '(API)')) {
0 ignored issues
show
Bug introduced by
It seems like $entityform can also be of type null; however, parameter $haystack of str_contains() does only seem to accept string, 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

199
            if (\str_contains(/** @scrutinizer ignore-type */ $entityform, '(API)')) {
Loading history...
200
                $isApi = true;
201
                $entityform = trim(\str_replace('(API)', '', $entityform));
202
            }
203
204
            // Setup an utility pack of commands (Commands.php)
205 1
            $command = $this->pacommands;
206 1
            $result = $command->generateFormCrud($entityform, $generatemplate, $isApi);
207
208 1
            $this->locksystem->release();
209
            //$retcc = '';
210 1
            if ($result['errcode'] < 0) {
211
                $twigparms = array('errcode' => $result['errcode'], 'command' => 'Generazione Form Crud', 'message' => $result['message']);
212
213
                return new Response(
214
                    $this->renderView('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms),
215
                    500
216
                );
217
            } else {
218
                //$retcc = $command->clearCacheEnv($this->get('kernel')->getEnvironment());
219
            }
220 1
            $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
221
222 1
            return $this->render('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
223
        }
224
    }
225
226
    /* ENTITIES */
227
228 1
    public function generateEntity(Request $request)
229
    {
230 1
        if (!$this->locksystem->acquire()) {
231
            return new Response($this->getLockMessage());
232
        } else {
233 1
            $this->locksystem->acquire();
234 1
            $wbFile = $request->get('file');
235 1
            $command = $this->pacommands;
236 1
            $result = $command->generateEntity($wbFile);
237 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...
238 1
            $this->locksystem->release();
239 1
            if (0 != $result['errcode']) {
240 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
241 1
                $view = $this->renderView('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
242
243 1
                return new Response($view, 500);
244
            } else {
245 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
246
247 1
                return $this->render('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
248
            }
249
        }
250
    }
251
252
    /* VCS (GIT,SVN) */
253
254
    /**
255
     * @codeCoverageIgnore
256
     */
257
    public function getVcs()
258
    {
259
        set_time_limit(0);
260
        $this->apppaths = $this->apppaths;
261
        if (!$this->locksystem->acquire()) {
262
            return new Response($this->getLockMessage());
263
        } else {
264
            $this->locksystem->acquire();
265
            $command = $this->pacommands;
266
            $result = $command->getVcs();
267
            $this->locksystem->release();
268
            $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
269
270
            return $this->render('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
271
        }
272
    }
273
274
    /* CLEAR CACHE */
275
276
    /**
277
     * Suppress PMD warnings per exit.
278
     *
279
     * @//SuppressWarnings(PHPMD)
280
     */
281
    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

281
    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...
282
    {
283
        set_time_limit(0);
284
        if (!$this->locksystem->acquire()) {
285
            return new Response($this->getLockMessage());
286
        } else {
287
            $this->locksystem->acquire();
288
            $command = $this->pacommands;
289
            $result = $command->clearcache();
290
291
            $this->locksystem->release();
292
293
            if (0 != $result['errcode']) {
294
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
295
                $view = $this->renderView('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
296
297
                return new Response($view, 500);
298
            } else {
299
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
300
301
                return $this->render('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
302
            }
303
        }
304
    }
305
306
    /* CLEAR CACHE */
307
308 1
    public function symfonyCommand(Request $request)
309
    {
310 1
        set_time_limit(0);
311
312 1
        $simfonycommand = $request->get('symfonycommand');
313 1
        if (!$this->locksystem->acquire()) {
314
            return new Response($this->getLockMessage());
315
        } else {
316 1
            $this->locksystem->acquire();
317 1
            $this->apppaths = $this->apppaths;
318 1
            $pammutils = $this->pautils;
319 1
            $command = $this->apppaths->getConsoleExecute().' '.$simfonycommand;
320 1
            $result = $pammutils->runCommand($command);
321
322 1
            $this->locksystem->release();
323 1
            if (0 != $result['errcode']) {
324 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
325 1
                $view = $this->renderView('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
326
327 1
                return new Response($view, 500);
328
            } else {
329 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
330
331 1
                return $this->render('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
332
            }
333
        }
334
    }
335
336
    /**
337
     * Suppress PMD warnings per exit.
338
     *
339
     * @SuppressWarnings(PHPMD)
340
     */
341 1
    public function unixCommand(Request $request)
342
    {
343 1
        set_time_limit(0);
344 1
        $pammutils = $this->pautils;
345 1
        $unixcommand = $request->get('unixcommand');
346
        //Se viene lanciato il comando per cancellare il file di lock su bypassa tutto e si lancia
347 1
        $dellockfile = 'DELETELOCK';
348 1
        if ($unixcommand == $dellockfile) {
349
            $this->locksystem->release();
350
351
            return new Response('File di lock cancellato');
352
        }
353
354 1
        if (!$this->locksystem->acquire()) {
355
            return new Response($this->getLockMessage());
356
        } else {
357 1
            $this->locksystem->acquire();
358 1
            $result = $pammutils->runCommand($unixcommand);
359
360 1
            $this->locksystem->release();
361
            // eseguito deopo la fine del comando
362 1
            if (0 != $result['errcode']) {
363 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
364 1
                $view = $this->renderView('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
365
366 1
                return new Response($view, 500);
367
            } else {
368 1
                $twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']);
369
370 1
                return $this->render('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
371
            }
372
        }
373
    }
374
375
    /**
376
     * @codeCoverageIgnore
377
     */
378
    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

378
    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...
379
    {
380
        set_time_limit(0);
381
        $this->apppaths = $this->apppaths;
382
        if (!$this->locksystem->acquire()) {
383
            return new Response($this->getLockMessage());
384
        } else {
385
            if (!OsFunctions::isWindows()) {
386
                $this->locksystem->acquire();
387
                //$phpPath = OsFunctions::getPHPExecutableFromPath();
388
                $command = 'vendor'.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'simple-phpunit';
389
                $process = new Process(array($command));
390
                $process->setWorkingDirectory($this->apppaths->getRootPath());
391
392
                $process->run();
393
394
                $this->locksystem->release();
395
                // eseguito dopo la fine del comando
396
                if (!$process->isSuccessful()) {
397
                    $twigparms = array('errcode' => -1, 'command' => $command, 'message' => $process->getOutput().$process->getErrorOutput());
398
                    $view = $this->renderView('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
399
400
                    return new Response($view, 500);
401
                } else {
402
                    $twigparms = array('errcode' => 0, 'command' => $command, 'message' => $process->getOutput().$process->getErrorOutput());
403
404
                    return $this->render('@PannelloAmministrazione/PannelloAmministrazione/outputcommand.html.twig', $twigparms);
405
                }
406
            } else {
407
                // @codeCoverageIgnoreStart
408
                return new Response('Non previsto in ambiente windows!', 500);
409
                // @codeCoverageIgnoreEnd
410
            }
411
        }
412
    }
413
}
414