Completed
Push — master ( d32927...a316bd )
by Andrea
11:24
created

cleanTemplatePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\DependencyInjection;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Fi\OsBundle\DependencyInjection\OsFunctions;
7
8
class PannelloamministrazioneCommands
9
{
10
11
    private $container;
12
    private $apppaths;
13
    private $pammutils;
14
15 1
    public function __construct($container)
16
    {
17 1
        $this->container = $container;
18 1
        $this->apppaths = $container->get("pannelloamministrazione.projectpath");
19 1
        $this->pammutils = $container->get("pannelloamministrazione.utils");
20 1
    }
21
22 1
    public function getVcs()
23
    {
24 1
        $fs = new Filesystem();
25
26 1
        $sepchr = OsFunctions::getSeparator();
27 1
        $projectDir = $this->apppaths->getRootPath();
28 1
        $vcscommand = "";
29 1
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.svn')) {
30
            $vcscommand = 'svn update';
31
        }
32 1
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.git')) {
33
            $vcscommand = 'git pull';
34
        }
35 1
        if (!$vcscommand) {
36 1
            throw new \Exception("Vcs non trovato", 100);
37
        }
38
        $command = 'cd ' . $projectDir . $sepchr . $vcscommand;
39
        return $this->pammutils->runCommand($command);
40
    }
41
42
    public function generateBundle($bundleName)
43
    {
44
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
45
        $fs = new Filesystem();
46
47
        $srcPath = $this->apppaths->getSrcPath();
48
49
        $bundlePath = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundleName;
50
51
        $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...
52
53
        if ($fs->exists($bundlePath)) {
54
            return array('errcode' => -1, 'command' => 'generate:bundle', 'message' => "Il bundle esiste gia' in $bundlePath");
55
        }
56
//        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...
57
//            return array('errcode' => -1, 'command' => 'generate:bundle', 'message' => "$bundlePath non scrivibile");
58
//        }
59
60
        $commandparms = array(
61
            '--namespace' => $bundleName,
62
            '--dir' => $srcPath . DIRECTORY_SEPARATOR,
63
            '--format' => 'yml',
64
            '--env' => $this->container->get('kernel')->getEnvironment(),
65
            '--no-interaction' => true,
66
            '--no-debug' => true,
67
        );
68
        $result = $this->pammutils->runSymfonyCommand('generate:bundle', $commandparms);
69
        $bundlePath = $srcPath . DIRECTORY_SEPARATOR . $bundleName;
70
        if ($fs->exists($bundlePath)) {
71
            $addmessage = 'Per abilitare il nuovo bundle nel kernel controllare che sia presente in app/AppKernel.php, '
72
                    . 'pulire la cache e aggiornare la pagina';
73
            $ret = array('errcode' => 0, 'command' => 'generate:bundle', 'message' => $result["message"] . $addmessage);
74
        } else {
75
            $addmessage = "Non e' stato creato il bundle in $bundlePath";
76
            $ret = array('errcode' => -1, 'command' => 'generate:bundle', 'message' => $result["message"] . $addmessage);
77
        }
78
        return $ret;
79
    }
80
81
    public function generateEntity($wbFile, $bundlePath)
82
    {
83
        $command = "pannelloamministrazione:generateymlentities";
84
        $result = $this->pammutils->runSymfonyCommand($command, array('mwbfile' => $wbFile, 'bundlename' => $bundlePath));
85
86
        if ($result["errcode"] != 0) {
87
            return array(
88
                'errcode' => -1,
89
                'message' => 'Errore nel comando: <i style="color: white;">' .
90
                $command . '</i><br/><i style="color: red;">' .
91
                str_replace("\n", '<br/>', $result["message"]) .
92
                'in caso di errori eseguire il comando symfony non da web: pannelloamministrazione:generateymlentities ' .
93
                $wbFile . ' ' . $bundlePath . '<br/></i>',
94
            );
95
        }
96
97
        return array(
98
            'errcode' => 0,
99
            'message' => '<pre>Eseguito comando: <i style = "color: white;">' .
100
            $command . '</i><br/>' . str_replace("\n", '<br/>', $result["message"]) . '</pre>',);
101
    }
102
103
    public function generateEntityClass($bundlePath)
104
    {
105
        $command = "pannelloamministrazione:generateentities";
106
        $result = $this->pammutils->runSymfonyCommand($command, array('bundlename' => $bundlePath));
107
108
        if ($result["errcode"] != 0) {
109
            return array(
110
                'errcode' => -1,
111
                'message' => 'Errore nel comando: <i style="color: white;">' .
112
                $command . '</i><br/><i style="color: red;">' .
113
                str_replace("\n", '<br/>', $result["message"]) .
114
                'in caso di errori eseguire il comando symfony non da web: pannelloamministrazione:generateentities ' .
115
                $bundlePath . '<br/>Opzione --schemaupdate oer aggiornare anche lo schema database</i>',
116
            );
117
        }
118
119
        return array(
120
            'errcode' => 0,
121
            'message' => '<pre>Eseguito comando: <i style = "color: white;">' .
122
            $command . '</i><br/>' . str_replace("\n", '<br/>', $result["message"]) . '</pre>',);
123
    }
124
125
    public function generateFormCrud($bundlename, $entityform)
126
    {
127
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
128
        $fs = new Filesystem();
129
        $resultchk = $this->checkFormCrud($bundlename, $entityform);
130
131
        if ($resultchk["errcode"] != 0) {
132
            return $resultchk;
133
        }
134
        $crudparms = array(
135
            'entity' => str_replace('/', '', $bundlename) . ':' . $entityform,
136
            '--route-prefix' => $entityform,
137
            "--env" => $this->container->get('kernel')->getEnvironment(),
138
            '--with-write' => true,
139
            '--format' => 'yml',
140
            '--overwrite' => false,
141
            '--no-interaction' => true,
142
            '--no-debug' => true);
143
144
        $resultcrud = $this->pammutils->runSymfonyCommand('doctrine:generate:crud', $crudparms);
145
146
        if ($resultcrud['errcode'] == 0) {
147
            if ($fs->exists($viewPathSrc)) {
148
                $fs->remove($viewPathSrc);
0 ignored issues
show
Bug introduced by
The variable $viewPathSrc does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
149
            }
150
            $formcrudparms = array("bundlename" => $bundlename, "entityform" => $entityform);
151
152
            $retmsggenerateform = $this->pammutils->runSymfonyCommand('pannelloamministrazione:generateformcrud', $formcrudparms);
153
154
            $retmsg = array(
155
                'errcode' => 0,
156
                'command' => $resultcrud['command'],
157
                'message' => $resultcrud['message'] . $retmsggenerateform['message'],
158
            );
159
        } else {
160
            $retmsg = array(
161
                'errcode' => $resultcrud['errcode'],
162
                'command' => $resultcrud['command'],
163
                'message' => $resultcrud['message'],
164
            );
165
        }
166
167
        return $retmsg;
168
    }
169
170
    public function checkFormCrud($bundlename, $entityform)
171
    {
172
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
173
        $fs = new Filesystem();
174
        $srcPath = $this->apppaths->getSrcPath();
175
        $appPath = $this->apppaths->getAppPath();
176
        if (!is_writable($appPath)) {
177
            return array('errcode' => -1, 'message' => $appPath . ' non scrivibile');
178
        }
179
        $formPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
180
                'Form' . DIRECTORY_SEPARATOR . $entityform . 'Type.php';
181
182
        if ($fs->exists($formPath)) {
183
            return array('errcode' => -1, 'message' => $formPath . ' esistente');
184
        }
185
186
        $controllerPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
187
                'Controller' . DIRECTORY_SEPARATOR . $entityform . 'Controller.php';
188
189
        if ($fs->exists($controllerPath)) {
190
            return array('errcode' => -1, 'message' => $controllerPath . ' esistente');
191
        }
192
193
        $viewPathSrc = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
194
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $entityform;
195
196
        if ($fs->exists($viewPathSrc)) {
197
            return array('errcode' => -1, 'message' => $viewPathSrc . ' esistente');
198
        }
199
200
        return array('errcode' => 0, 'message' => 'OK');
201
    }
202
203
    public function clearcache()
204
    {
205
        $cmdoutput = "";
206
        $envs = array("dev", "test", "prod");
207
        foreach ($envs as $env) {
208
            $cmdoutput = $cmdoutput . $this->clearcacheEnv($env);
209
        }
210
        //$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...
211
212
        return $cmdoutput;
213
    }
214
215
    public function clearcacheEnv($env)
216
    {
217
        $ret = $this->pammutils->clearcache($env);
218
219
        return $ret["errmsg"];
220
    }
221
222
    public function aggiornaSchemaDatabase()
223
    {
224
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
225
226
        return $result;
227
    }
228
}
229