Completed
Push — master ( c6735e...5bf063 )
by Andrea
09:55
created

Commands   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 10.29%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 7
dl 0
loc 249
ccs 14
cts 136
cp 0.1029
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
B generateBundle() 0 40 3
B generateEntity() 0 32 3
B generateEntityClass() 0 33 3
B generateFormCrud() 0 67 6
A cleanTemplatePath() 0 12 3
A clearcache() 0 11 2
A clearcacheEnv() 0 6 1
A aggiornaSchemaDatabase() 0 6 1
A __construct() 0 6 1
A getVcs() 0 19 4
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 1
    public function __construct($container)
19
    {
20 1
        $this->container = $container;
21 1
        $this->apppaths = new ProjectPath($container);
22 1
        $this->pammutils = new PannelloAmministrazioneUtils($container);
23 1
    }
24
25 1
    public function getVcs()
26
    {
27 1
        $fs = new Filesystem();
28
29 1
        $sepchr = OsFunctions::getSeparator();
30 1
        $projectDir = $this->apppaths->getRootPath();
31 1
        $vcscommand = "";
32 1
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.svn')) {
33
            $vcscommand = 'svn update';
34
        }
35 1
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.git')) {
36
            $vcscommand = 'git pull';
37
        }
38 1
        if (!$vcscommand) {
39 1
            throw new \Exception("Vcs non trovato", 100);
40
        }
41
        $command = 'cd ' . $projectDir . $sepchr . $vcscommand;
42
        return $this->pammutils->runCommand($command);
43
    }
44
45
    public function generateBundle($bundleName)
46
    {
47
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
48
        $fs = new Filesystem();
49
50
        $commands = new PannelloAmministrazioneUtils($this->container);
51
52
        $srcPath = $this->apppaths->getSrcPath();
53
54
        $bundlePath = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundleName;
55
56
        $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...
57
58
        if ($fs->exists($bundlePath)) {
59
            return array('errcode' => -1, 'command' => 'generate:bundle', 'message' => "Il bundle esiste gia' in $bundlePath");
60
        }
61
//        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...
62
//            return array('errcode' => -1, 'command' => 'generate:bundle', 'message' => "$bundlePath non scrivibile");
63
//        }
64
65
        $commandparms = array(
66
            '--namespace' => $bundleName,
67
            '--dir' => $srcPath . DIRECTORY_SEPARATOR,
68
            '--format' => 'yml',
69
            '--env' => $this->container->get('kernel')->getEnvironment(),
70
            '--no-interaction' => true,
71
            '--no-debug' => true,
72
        );
73
        $result = $commands->runSymfonyCommand('generate:bundle', $commandparms);
74
        $bundlePath = $srcPath . DIRECTORY_SEPARATOR . $bundleName;
75
        if ($fs->exists($bundlePath)) {
76
            $addmessage = 'Per abilitare il nuovo bundle nel kernel controllare che sia presente in app/AppKernel.php, '
77
                    . 'pulire la cache e aggiornare la pagina';
78
            $ret = array('errcode' => 0, 'command' => 'generate:bundle', 'message' => $result["message"] . $addmessage);
79
        } else {
80
            $addmessage = "Non e' stato creato il bundle in $bundlePath";
81
            $ret = array('errcode' => -1, 'command' => 'generate:bundle', 'message' => $result["message"] . $addmessage);
82
        }
83
        return $ret;
84
    }
85
86
    public function generateEntity($wbFile, $bundlePath)
87
    {
88
        $console = $this->apppaths->getConsole();
89
        $pannellocmd = "pannelloamministrazione:generateymlentities $wbFile $bundlePath ";
90
91
        $scriptGenerator = $console . " " . $pannellocmd;
92
93
        $phpPath = OsFunctions::getPHPExecutableFromPath();
94
        $sepchr = OsFunctions::getSeparator();
95
96
        $command = 'cd ' . $this->apppaths->getRootPath() . $sepchr
97
                . $phpPath . ' ' . $scriptGenerator . ' --no-debug --env=' . $this->container->get('kernel')->getEnvironment();
98
        $process = new Process($command);
99
        $process->setTimeout(60 * 100);
100
        $process->run();
101
102
        if (!$process->isSuccessful()) {
103
            return array(
104
                'errcode' => -1,
105
                'message' => 'Errore nel comando: <i style="color: white;">' .
106
                $command . '</i><br/><i style="color: red;">' .
107
                str_replace("\n", '<br/>', ($process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput())) .
108
                'in caso di errori eseguire il comando symfony non da web: pannelloamministrazione:generateymlentities ' .
109
                $wbFile . ' ' . $bundlePath . '<br/></i>',
110
            );
111
        }
112
113
        return array(
114
            'errcode' => 0,
115
            'message' => '<pre>Eseguito comando: <i style = "color: white;">' .
116
            $command . '</i><br/>' . str_replace("\n", '<br/>', $process->getOutput()) . '</pre>',);
117
    }
118
119
    public function generateEntityClass($bundlePath)
120
    {
121
        $console = $this->apppaths->getConsole();
122
        $pannellocmd = "pannelloamministrazione:generateentities $bundlePath";
123
124
        $scriptGenerator = $console . " " . $pannellocmd;
125
126
        $phpPath = OsFunctions::getPHPExecutableFromPath();
127
        $sepchr = OsFunctions::getSeparator();
128
129
        $command = 'cd ' . $this->apppaths->getRootPath() . $sepchr
130
                . $phpPath . ' ' . $scriptGenerator . ' --env=' . $this->container->get('kernel')->getEnvironment();
131
132
        $process = new Process($command);
133
        $process->setTimeout(60 * 100);
134
        $process->run();
135
136
        if (!$process->isSuccessful()) {
137
            return array(
138
                'errcode' => -1,
139
                'message' => 'Errore nel comando: <i style="color: white;">' .
140
                $command . '</i><br/><i style="color: red;">' .
141
                str_replace("\n", '<br/>', ($process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput())) .
142
                'in caso di errori eseguire il comando symfony non da web: pannelloamministrazione:generateentities ' .
143
                $bundlePath . '<br/>Opzione --schemaupdate oer aggiornare anche lo schema database</i>',
144
            );
145
        }
146
147
        return array(
148
            'errcode' => 0,
149
            'message' => '<pre>Eseguito comando: <i style = "color: white;">' .
150
            $command . '</i><br/>' . str_replace("\n", '<br/>', $process->getOutput()) . '</pre>',);
151
    }
152
153
    public function generateFormCrud($bundlename, $entityform)
154
    {
155
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
156
        $fs = new Filesystem();
157
        $srcPath = $this->apppaths->getSrcPath();
158
        $appPath = $this->apppaths->getAppPath();
159
        if (!is_writable($appPath)) {
160
            return array('errcode' => -1, 'message' => $appPath . ' non scrivibile');
161
        }
162
        $formPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
163
                'Form' . DIRECTORY_SEPARATOR . $entityform . 'Type.php';
164
165
        if ($fs->exists($formPath)) {
166
            return array('errcode' => -1, 'message' => $formPath . ' esistente');
167
        }
168
169
        $controllerPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
170
                'Controller' . DIRECTORY_SEPARATOR . $entityform . 'Controller.php';
171
172
        if ($fs->exists($controllerPath)) {
173
            return array('errcode' => -1, 'message' => $controllerPath . ' esistente');
174
        }
175
176
        $viewPathSrc = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
177
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $entityform;
178
179
        if ($fs->exists($viewPathSrc)) {
180
            return array('errcode' => -1, 'message' => $viewPathSrc . ' esistente');
181
        }
182
183
        $crudparms = array(
184
            '--entity' => str_replace('/', '', $bundlename) . ':' . $entityform,
185
            '--route-prefix' => $entityform,
186
            "--env" => $this->container->get('kernel')->getEnvironment(),
187
            '--with-write' => true, '--format' => 'yml', '--overwrite' => false, '--no-interaction' => true,);
188
189
        $resultcrud = $this->pammutils->runSymfonyCommand('doctrine:generate:crud', $crudparms);
190
191
        if ($resultcrud['errcode'] == 0) {
192
            $fs->remove($viewPathSrc);
193
            $generator = new GenerateCode($this->container);
194
195
            $retmsggenerateform = $generator->generateFormsTemplates($bundlename, $entityform);
196
197
            $generator->generateFormsDefaultTableValues($entityform);
198
199
            $appviews = $appPath . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views';
200
            $this->cleanTemplatePath($appviews);
201
202
            $resourcesviews = $appPath . DIRECTORY_SEPARATOR . 'Resources';
203
            $this->cleanTemplatePath($resourcesviews);
204
205
            $retmsg = array(
206
                'errcode' => 0,
207
                'command' => $resultcrud['command'],
208
                'message' => $resultcrud['message'] . $retmsggenerateform,
209
            );
210
        } else {
211
            $retmsg = array(
212
                'errcode' => $resultcrud['errcode'],
213
                'command' => $resultcrud['command'],
214
                'message' => $resultcrud['message'],
215
            );
216
        }
217
218
        return $retmsg;
219
    }
220
221
    private function cleanTemplatePath($path)
222
    {
223
        $fs = new Filesystem();
224
        $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...
225
        if ($fs->exists($path)) {
226
            $finder = new Finder();
227
            $ret = $finder->files()->in($path);
228
            if (count($ret) == 0) {
229
                $fs->remove($path);
230
            }
231
        }
232
    }
233
234
    public function clearcache()
235
    {
236
        $cmdoutput = "";
237
        $envs = array("dev", "test", "prod");
238
        foreach ($envs as $env) {
239
            $cmdoutput = $cmdoutput . $this->clearcacheEnv($env);
240
        }
241
        //$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...
242
243
        return $cmdoutput;
244
    }
245
246
    public function clearcacheEnv($env)
247
    {
248
        $ret = $this->pammutils->clearcache($env);
249
250
        return $ret["errmsg"];
251
    }
252
253
    public function aggiornaSchemaDatabase()
254
    {
255
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
256
257
        return $result;
258
    }
259
}
260