Completed
Push — master ( 9b53e2...2758ff )
by Andrea
19:08 queued 08:29
created

PannelloamministrazioneCommands   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 12.83%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 2
dl 0
loc 195
ccs 14
cts 109
cp 0.1283
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getVcs() 0 19 4
B generateBundle() 0 36 3
A generateEntity() 0 21 2
A generateEntityClass() 0 21 2
A generateFormCrud() 0 20 2
B checkFormCrud() 0 32 5
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\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
        if ($fs->exists($bundlePath)) {
52
            return array('errcode' => -1, 'command' => 'generate:bundle', 'message' => "Il bundle esiste gia' in $bundlePath");
53
        }
54
//        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...
55
//            return array('errcode' => -1, 'command' => 'generate:bundle', 'message' => "$bundlePath non scrivibile");
56
//        }
57
58
        $commandparms = array(
59
            '--namespace' => $bundleName,
60
            '--dir' => $srcPath . DIRECTORY_SEPARATOR,
61
            '--format' => 'yml',
62
            '--env' => $this->container->get('kernel')->getEnvironment(),
63
            '--no-interaction' => true,
64
            '--no-debug' => true,
65
        );
66
        $result = $this->pammutils->runSymfonyCommand('generate:bundle', $commandparms);
67
        $bundlePath = $srcPath . DIRECTORY_SEPARATOR . $bundleName;
68
        if ($fs->exists($bundlePath)) {
69
            $addmessage = 'Per abilitare il nuovo bundle nel kernel controllare che sia presente in app/AppKernel.php, '
70
                    . 'pulire la cache e aggiornare la pagina';
71
            $ret = array('errcode' => 0, 'command' => 'generate:bundle', 'message' => $result["message"] . $addmessage);
72
        } else {
73
            $addmessage = "Non e' stato creato il bundle in $bundlePath";
74
            $ret = array('errcode' => -1, 'command' => 'generate:bundle', 'message' => $result["message"] . $addmessage);
75
        }
76
        return $ret;
77
    }
78
79
    public function generateEntity($wbFile, $bundlePath)
80
    {
81
        $command = "pannelloamministrazione:generateymlentities";
82
        $result = $this->pammutils->runSymfonyCommand($command, array('mwbfile' => $wbFile, 'bundlename' => $bundlePath));
83
84
        if ($result["errcode"] != 0) {
85
            return array(
86
                'errcode' => -1,
87
                'message' => 'Errore nel comando: <i style="color: white;">' .
88
                $command . '</i><br/><i style="color: red;">' .
89
                str_replace("\n", '<br/>', $result["message"]) .
90
                'in caso di errori eseguire il comando symfony non da web: pannelloamministrazione:generateymlentities ' .
91
                $wbFile . ' ' . $bundlePath . '<br/></i>',
92
            );
93
        }
94
95
        return array(
96
            'errcode' => 0,
97
            'message' => '<pre>Eseguito comando: <i style = "color: white;">' .
98
            $command . '</i><br/>' . str_replace("\n", '<br/>', $result["message"]) . '</pre>',);
99
    }
100
101
    public function generateEntityClass($bundlePath)
102
    {
103
        $command = "pannelloamministrazione:generateentities";
104
        $result = $this->pammutils->runSymfonyCommand($command, array('bundlename' => $bundlePath));
105
106
        if ($result["errcode"] != 0) {
107
            return array(
108
                'errcode' => -1,
109
                'message' => 'Errore nel comando: <i style="color: white;">' .
110
                $command . '</i><br/><i style="color: red;">' .
111
                str_replace("\n", '<br/>', $result["message"]) .
112
                'in caso di errori eseguire il comando symfony non da web: pannelloamministrazione:generateentities ' .
113
                $bundlePath . '<br/>Opzione --schemaupdate oer aggiornare anche lo schema database</i>',
114
            );
115
        }
116
117
        return array(
118
            'errcode' => 0,
119
            'message' => '<pre>Eseguito comando: <i style = "color: white;">' .
120
            $command . '</i><br/>' . str_replace("\n", '<br/>', $result["message"]) . '</pre>',);
121
    }
122
123
    public function generateFormCrud($bundlename, $entityform)
124
    {
125
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
126
        $resultchk = $this->checkFormCrud($bundlename, $entityform);
127
128
        if ($resultchk["errcode"] != 0) {
129
            return $resultchk;
130
        }
131
        $formcrudparms = array("bundlename" => $bundlename, "entityform" => $entityform);
132
133
        $retmsggenerateform = $this->pammutils->runSymfonyCommand('pannelloamministrazione:generateformcrud', $formcrudparms);
134
135
        $retmsg = array(
136
            'errcode' => $retmsggenerateform['errcode'],
137
            'command' => $retmsggenerateform['command'],
138
            'message' => $retmsggenerateform['message'],
139
        );
140
141
        return $retmsg;
142
    }
143
144
    public function checkFormCrud($bundlename, $entityform)
145
    {
146
        /* @var $fs \Symfony\Component\Filesystem\Filesystem */
147
        $fs = new Filesystem();
148
        $srcPath = $this->apppaths->getSrcPath();
149
        $appPath = $this->apppaths->getAppPath();
150
        if (!is_writable($appPath)) {
151
            return array('errcode' => -1, 'message' => $appPath . ' non scrivibile');
152
        }
153
        $formPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
154
                'Form' . DIRECTORY_SEPARATOR . $entityform . 'Type.php';
155
156
        if ($fs->exists($formPath)) {
157
            return array('errcode' => -1, 'message' => $formPath . ' esistente');
158
        }
159
160
        $controllerPath = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
161
                'Controller' . DIRECTORY_SEPARATOR . $entityform . 'Controller.php';
162
163
        if ($fs->exists($controllerPath)) {
164
            return array('errcode' => -1, 'message' => $controllerPath . ' esistente');
165
        }
166
167
        $viewPathSrc = $srcPath . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
168
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $entityform;
169
170
        if ($fs->exists($viewPathSrc)) {
171
            return array('errcode' => -1, 'message' => $viewPathSrc . ' esistente');
172
        }
173
174
        return array('errcode' => 0, 'message' => 'OK');
175
    }
176
177
    public function clearcache()
178
    {
179
        $cmdoutput = "";
180
        $envs = array("dev", "test", "prod");
181
        foreach ($envs as $env) {
182
            $cmdoutput = $cmdoutput . $this->clearcacheEnv($env);
183
        }
184
        //$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...
185
186
        return $cmdoutput;
187
    }
188
189
    public function clearcacheEnv($env)
190
    {
191
        $ret = $this->pammutils->clearcache($env);
192
193
        return $ret["errmsg"];
194
    }
195
196
    public function aggiornaSchemaDatabase()
197
    {
198
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
199
200
        return $result;
201
    }
202
}
203