Completed
Push — master ( b8d570...a2cf15 )
by Andrea
11:41
created

generateFormCrud()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 73
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 73
ccs 0
cts 48
cp 0
rs 6.6896
cc 7
eloc 48
nc 7
nop 2
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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