Completed
Push — master ( 175c72...a3c945 )
by Andrea
08:57 queued 10s
created

PannelloamministrazioneCommands::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 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, '--format' => 'yml', '--overwrite' => false, '--no-interaction' => true,);
161
162
        $resultcrud = $this->pammutils->runSymfonyCommand('doctrine:generate:crud', $crudparms);
163
164
        if ($resultcrud['errcode'] == 0) {
165
            $fs->remove($viewPathSrc);
166
            $generator = $this->container->get("pannelloamministrazione.generateform");
167
168
            $retmsggenerateform = $generator->generateFormsTemplates($bundlename, $entityform);
169
170
            $generator->generateFormsDefaultTableValues($entityform);
171
172
            $appviews = $appPath . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views';
173
            $this->cleanTemplatePath($appviews);
174
175
            $resourcesviews = $appPath . DIRECTORY_SEPARATOR . 'Resources';
176
            $this->cleanTemplatePath($resourcesviews);
177
178
            $retmsg = array(
179
                'errcode' => 0,
180
                'command' => $resultcrud['command'],
181
                'message' => $resultcrud['message'] . $retmsggenerateform,
182
            );
183
        } else {
184
            $retmsg = array(
185
                'errcode' => $resultcrud['errcode'],
186
                'command' => $resultcrud['command'],
187
                'message' => $resultcrud['message'],
188
            );
189
        }
190
191
        return $retmsg;
192
    }
193
194
    private function cleanTemplatePath($path)
195
    {
196
        $fs = new Filesystem();
197
        $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...
198
        if ($fs->exists($path)) {
199
            $finder = new Finder();
200
            $ret = $finder->files()->in($path);
201
            if (count($ret) == 0) {
202
                $fs->remove($path);
203
            }
204
        }
205
    }
206
207
    public function clearcache()
208
    {
209
        $cmdoutput = "";
210
        $envs = array("dev", "test", "prod");
211
        foreach ($envs as $env) {
212
            $cmdoutput = $cmdoutput . $this->clearcacheEnv($env);
213
        }
214
        //$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...
215
216
        return $cmdoutput;
217
    }
218
219
    public function clearcacheEnv($env)
220
    {
221
        $ret = $this->pammutils->clearcache($env);
222
223
        return $ret["errmsg"];
224
    }
225
226
    public function aggiornaSchemaDatabase()
227
    {
228
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
229
230
        return $result;
231
    }
232
}
233