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

PannelloamministrazioneCommands   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 10.07%

Importance

Changes 0
Metric Value
wmc 26
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 230
ccs 14
cts 139
cp 0.1007
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getVcs() 0 19 4
B generateBundle() 0 38 3
A generateEntity() 0 21 2
A generateEntityClass() 0 21 2
C generateFormCrud() 0 73 7
A cleanTemplatePath() 0 12 3
A clearcache() 0 11 2
A clearcacheEnv() 0 6 1
A aggiornaSchemaDatabase() 0 6 1
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