Completed
Push — master ( 58cbfb...3697fd )
by Andrea
47:14 queued 44:56
created

generateFormCrud()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 2
crap 6
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
        $resultchk = $this->checkFormCrud($bundlename, $entityform);
129
130
        if ($resultchk["errcode"] != 0) {
131
            return $resultchk;
132
        }
133
        $formcrudparms = array("bundlename" => $bundlename, "entityform" => $entityform);
134
135
        $retmsggenerateform = $this->pammutils->runSymfonyCommand('pannelloamministrazione:generateformcrud', $formcrudparms);
136
137
        $retmsg = array(
138
            'errcode' => 0,
139
            'command' => $resultcrud['command'],
0 ignored issues
show
Bug introduced by
The variable $resultcrud 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...
140
            'message' => $resultcrud['message'] . $retmsggenerateform['message'],
141
        );
142
143
        return $retmsg;
144
    }
145
146
    public function checkFormCrud($bundlename, $entityform)
147
    {
148
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
149
        $fs = new Filesystem();
150
        $srcPath = $this->apppaths->getSrcPath();
151
        $appPath = $this->apppaths->getAppPath();
152
        if (!is_writable($appPath)) {
153
            return array('errcode' => -1, 'message' => $appPath . ' non scrivibile');
154
        }
155
        $formPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
156
                'Form' . DIRECTORY_SEPARATOR . $entityform . 'Type.php';
157
158
        if ($fs->exists($formPath)) {
159
            return array('errcode' => -1, 'message' => $formPath . ' esistente');
160
        }
161
162
        $controllerPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
163
                'Controller' . DIRECTORY_SEPARATOR . $entityform . 'Controller.php';
164
165
        if ($fs->exists($controllerPath)) {
166
            return array('errcode' => -1, 'message' => $controllerPath . ' esistente');
167
        }
168
169
        $viewPathSrc = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
170
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $entityform;
171
172
        if ($fs->exists($viewPathSrc)) {
173
            return array('errcode' => -1, 'message' => $viewPathSrc . ' esistente');
174
        }
175
176
        return array('errcode' => 0, 'message' => 'OK');
177
    }
178
179
    public function clearcache()
180
    {
181
        $cmdoutput = "";
182
        $envs = array("dev", "test", "prod");
183
        foreach ($envs as $env) {
184
            $cmdoutput = $cmdoutput . $this->clearcacheEnv($env);
185
        }
186
        //$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...
187
188
        return $cmdoutput;
189
    }
190
191
    public function clearcacheEnv($env)
192
    {
193
        $ret = $this->pammutils->clearcache($env);
194
195
        return $ret["errmsg"];
196
    }
197
198
    public function aggiornaSchemaDatabase()
199
    {
200
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
201
202
        return $result;
203
    }
204
}
205