Completed
Push — master ( 33af6b...56fca7 )
by Andrea
08:37
created

Commands::generateBundle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 20
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 25
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\DependencyInjection;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Fi\OsBundle\DependencyInjection\OsFunctions;
8
use Fi\PannelloAmministrazioneBundle\DependencyInjection\PannelloAmministrazioneUtils;
9
use Symfony\Component\Process\Process;
10
11
class Commands
12
{
13
14
    private $container;
15
    private $apppaths;
16
    private $pammutils;
17
18
    public function __construct($container)
19
    {
20
        $this->container = $container;
21
        $this->apppaths = new ProjectPath($container);
22
        $this->pammutils = new PannelloAmministrazioneUtils($container);
23
    }
24
25
    public function getVcs()
26
    {
27
        $fs = new Filesystem();
28
29
        $sepchr = OsFunctions::getSeparator();
30
        $projectDir = $this->apppaths->getRootPath();
31
        $vcscommand = "";
32
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.svn')) {
33
            $vcscommand = 'svn update';
34
        }
35
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.git')) {
36
            $vcscommand = 'git pull';
37
        }
38
        if (!$vcscommand) {
39
            return array("errcode" => -100,
40
                "command" => "get vcs sources",
41
                "errmsg" => "Vcs non trovato"
42
            );
43
        }
44
        $command = 'cd ' . $projectDir . $sepchr . $vcscommand;
45
        return $this->pammutils->runCommand($command);
46
    }
47
48
    public function generateBundle($bundleName)
49
    {
50
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
51
        $fs = new Filesystem();
52
53
        $commands = new PannelloAmministrazioneUtils($this->container);
54
55
        $srcPath = $this->apppaths->getSrcPath();
56
57
        $bundlePath = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundleName;
58
59
        $addmessage = '';
0 ignored issues
show
Unused Code introduced by
$addmessage is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
61
        if ($fs->exists($bundlePath)) {
62
            return array('errcode' => -1, 'command' => 'generate:bundle', 'message' => "Il bundle esiste gia' in $bundlePath");
63
        }
64
//        if (!is_writable($bundlePath)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
//            return array('errcode' => -1, 'command' => 'generate:bundle', 'message' => "$bundlePath non scrivibile");
66
//        }
67
68
        $commandparms = array(
69
            '--namespace' => $bundleName,
70
            '--dir' => $srcPath . DIRECTORY_SEPARATOR,
71
            '--format' => 'yml',
72
            '--env' => $this->container->get('kernel')->getEnvironment(),
73
            '--no-interaction' => true,
74
            '--no-debug' => true,
75
        );
76
        $result = $commands->runSymfonyCommand('generate:bundle', $commandparms);
77
        $bundlePath = $srcPath . DIRECTORY_SEPARATOR . $bundleName;
78
        if ($fs->exists($bundlePath)) {
79
            $addmessage = 'Per abilitare il nuovo bundle nel kernel controllare che sia presente in app/AppKernel.php, '
80
                    . 'pulire la cache e aggiornare la pagina';
81
            $ret = array('errcode' => 0, 'command' => 'generate:bundle', 'message' => $result["message"] . $addmessage);
82
        } else {
83
            $addmessage = "Non e' stato creato il bundle in $bundlePath";
84
            $ret = array('errcode' => -1, 'command' => 'generate:bundle', 'message' => $result["message"] . $addmessage);
85
        }
86
        return $ret;
87
    }
88
89
    public function generateEntity($wbFile, $bundlePath)
90
    {
91
        $console = $this->apppaths->getConsole();
92
        $pannellocmd = "pannelloamministrazione:generateymlentities $wbFile $bundlePath ";
93
94
        $scriptGenerator = $console . " " . $pannellocmd;
95
96
        $phpPath = OsFunctions::getPHPExecutableFromPath();
97
        $sepchr = OsFunctions::getSeparator();
98
99
        $command = 'cd ' . $this->apppaths->getRootPath() . $sepchr
100
                . $phpPath . ' ' . $scriptGenerator . ' --no-debug --env=' . $this->container->get('kernel')->getEnvironment();
101
        $process = new Process($command);
102
        $process->setTimeout(60 * 100);
103
        $process->run();
104
105
        if (!$process->isSuccessful()) {
106
            return array(
107
                'errcode' => -1,
108
                'message' => 'Errore nel comando: <i style="color: white;">' .
109
                $command . '</i><br/><i style="color: red;">' .
110
                str_replace("\n", '<br/>', ($process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput())) .
111
                'in caso di errori eseguire il comando symfony non da web: pannelloamministrazione:generateymlentities ' .
112
                $wbFile . ' ' . $bundlePath . '<br/></i>',
113
            );
114
        }
115
116
        return array(
117
            'errcode' => 0,
118
            'message' => '<pre>Eseguito comando: <i style = "color: white;">' .
119
            $command . '</i><br/>' . str_replace("\n", '<br/>', $process->getOutput()) . '</pre>',);
120
    }
121
122
    public function generateEntityClass($bundlePath)
123
    {
124
        $console = $this->apppaths->getConsole();
125
        $pannellocmd = "pannelloamministrazione:generateentities $bundlePath";
126
127
        $scriptGenerator = $console . " " . $pannellocmd;
128
129
        $phpPath = OsFunctions::getPHPExecutableFromPath();
130
        $sepchr = OsFunctions::getSeparator();
131
132
        $command = 'cd ' . $this->apppaths->getRootPath() . $sepchr
133
                . $phpPath . ' ' . $scriptGenerator . ' --env=' . $this->container->get('kernel')->getEnvironment();
134
135
        $process = new Process($command);
136
        $process->setTimeout(60 * 100);
137
        $process->run();
138
139
        if (!$process->isSuccessful()) {
140
            return array(
141
                'errcode' => -1,
142
                'message' => 'Errore nel comando: <i style="color: white;">' .
143
                $command . '</i><br/><i style="color: red;">' .
144
                str_replace("\n", '<br/>', ($process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput())) .
145
                'in caso di errori eseguire il comando symfony non da web: pannelloamministrazione:generateentities ' .
146
                $bundlePath . '<br/>Opzione --schemaupdate oer aggiornare anche lo schema database</i>',
147
            );
148
        }
149
150
        return array(
151
            'errcode' => 0,
152
            'message' => '<pre>Eseguito comando: <i style = "color: white;">' .
153
            $command . '</i><br/>' . str_replace("\n", '<br/>', $process->getOutput()) . '</pre>',);
154
    }
155
156
    public function generateFormCrud($bundlename, $entityform)
157
    {
158
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
159
        $fs = new Filesystem();
160
        $srcPath = $this->apppaths->getSrcPath();
161
        $appPath = $this->apppaths->getAppPath();
162
        if (!is_writable($appPath)) {
163
            return array('errcode' => -1, 'message' => $appPath . ' non scrivibile');
164
        }
165
        $formPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
166
                'Form' . DIRECTORY_SEPARATOR . $entityform . 'Type.php';
167
168
        if ($fs->exists($formPath)) {
169
            return array('errcode' => -1, 'message' => $formPath . ' esistente');
170
        }
171
172
        $controllerPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
173
                'Controller' . DIRECTORY_SEPARATOR . $entityform . 'Controller.php';
174
175
        if ($fs->exists($controllerPath)) {
176
            return array('errcode' => -1, 'message' => $controllerPath . ' esistente');
177
        }
178
179
        $viewPathSrc = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
180
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $entityform;
181
182
        if ($fs->exists($viewPathSrc)) {
183
            return array('errcode' => -1, 'message' => $viewPathSrc . ' esistente');
184
        }
185
186
        $crudparms = array(
187
            '--entity' => str_replace('/', '', $bundlename) . ':' . $entityform,
188
            '--route-prefix' => $entityform,
189
            "--env" => $this->container->get('kernel')->getEnvironment(),
190
            '--with-write' => true, '--format' => 'yml', '--overwrite' => false, '--no-interaction' => true,);
191
192
        $resultcrud = $this->pammutils->runSymfonyCommand('doctrine:generate:crud', $crudparms);
193
194
        if ($resultcrud['errcode'] == 0) {
195
            $fs->remove($viewPathSrc);
196
            $generator = new GenerateCode($this->container);
197
198
            $retmsggenerateform = $generator->generateFormsTemplates($bundlename, $entityform);
199
200
            $generator->generateFormsDefaultTableValues($entityform);
201
202
            $appviews = $appPath . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views';
203
            $this->cleanTemplatePath($appviews);
204
205
            $resourcesviews = $appPath . DIRECTORY_SEPARATOR . 'Resources';
206
            $this->cleanTemplatePath($resourcesviews);
207
208
            $retmsg = array(
209
                'errcode' => 0,
210
                'command' => $resultcrud['command'],
211
                'message' => $resultcrud['message'] . $retmsggenerateform,
212
            );
213
        } else {
214
            $retmsg = array(
215
                'errcode' => $resultcrud['errcode'],
216
                'command' => $resultcrud['command'],
217
                'message' => $resultcrud['message'],
218
            );
219
        }
220
221
        return $retmsg;
222
    }
223
224
    private function cleanTemplatePath($path)
225
    {
226
        $fs = new Filesystem();
227
        $ret = 0;
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
228
        if ($fs->exists($path)) {
229
            $finder = new Finder();
230
            $ret = $finder->files()->in($path);
231
            if (count($ret) == 0) {
232
                $fs->remove($path);
233
            }
234
        }
235
    }
236
237
    public function clearcache()
238
    {
239
        $cmdoutput = "";
240
        $envs = array("dev", "test", "prod");
241
        foreach ($envs as $env) {
242
            $cmdoutput = $cmdoutput . $this->clearcacheEnv($env);
243
        }
244
        //$cmdoutput = $cmdoutput . $this->clearcacheEnv($this->container->get('kernel')->getEnvironment());
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
245
246
        return $cmdoutput;
247
    }
248
249
    public function clearcacheEnv($env)
250
    {
251
        $ret = $this->pammutils->clearcache($env);
252
253
        return $ret["errmsg"];
254
    }
255
256
    public function aggiornaSchemaDatabase()
257
    {
258
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
259
260
        return $result;
261
    }
262
}
263