Commands::checkFormCrud()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.3696

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 8
eloc 24
c 3
b 1
f 0
nc 11
nop 3
dl 0
loc 42
ccs 16
cts 24
cp 0.6667
crap 10.3696
rs 8.4444
1
<?php
2
3
namespace Cdf\PannelloAmministrazioneBundle\Utils;
4
5
use Exception;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Cdf\BiCoreBundle\Utils\Api\ApiUtils;
8
use Cdf\PannelloAmministrazioneBundle\Utils\Utility;
9
10
class Commands
11
{
12
    /* @var $apppaths ProjectPath */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $apppaths at position 0 could not be parsed: Unknown type name '$apppaths' at position 0 in $apppaths.
Loading history...
13
14
    private ProjectPath $apppaths;
15
    /* @var $pammutils Utility */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $pammutils at position 0 could not be parsed: Unknown type name '$pammutils' at position 0 in $pammutils.
Loading history...
16
    private Utility $pammutils;
17
18 1
    public function __construct(ProjectPath $projectpath, Utility $pautils)
19
    {
20 1
        $this->apppaths = $projectpath;
21 1
        $this->pammutils = $pautils;
22
    }
23
24
    /**
25
     * @codeCoverageIgnoreStart
26
     *
27
     * @return array<mixed>
28
     * @throws \Exception
29
     */
30
    public function getVcs(): array
31
    {
32
        $command = "";
33
        $fs = new Filesystem();
34
35
        $projectDir = $this->apppaths->getRootPath();
36
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.svn')) {
37
            $command = 'svn update';
38
        }
39
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.git')) {
40
            $command = 'git pull';
41
        }
42
        if (!$command) {
43
            throw new Exception('Vcs non trovato', 100);
44
        }
45
46
        return $this->pammutils->runCommand($command, $projectDir);
47
    }
48
49
    /**
50
     *
51
     * @codeCoverageIgnoreEnd
52
     *
53
     * @return array<mixed>
54
     */
55
    public function generateEntity(string $wbFile): array
56
    {
57
        $command = 'pannelloamministrazione:generateormentities';
58
        $result = $this->pammutils->runSymfonyCommand($command, array('mwbfile' => $wbFile));
59
60
        if (0 != $result['errcode']) {
61
            return array(
62
                'errcode' => -1,
63
                'command' => $command,
64
                'message' => 'Errore nel comando:' . $command . ';' . $result['message'],
65
            );
66
        }
67
68
        return array(
69
            'errcode' => 0,
70
            'command' => $command,
71
            'message' => 'Eseguito comando:' . $command . ';' . $result['message'],);
72
    }
73
74
    /**
75
     *
76
     * @return array<mixed>
77
     */
78 1
    public function generateFormCrud(string $entityform, bool $generatemplate, bool $isAPI = false)
79
    {
80
        // check if some item already exist, and it interrupts the execution if any
81 1
        $pannelloamministrazioneentity = $entityform;
82
        /* @var $fs Filesystem */
83 1
        if ($isAPI) {
84
            $strposEntity = strpos($pannelloamministrazioneentity, ".");
85
            if ($strposEntity === false) {
86
                return array(
87
                    'errcode' => "-101",
88
                    'command' => "Ricerca entity",
89
                    'message' => "Impossibile trovare il . in " . $pannelloamministrazioneentity,
90
                );
91
            }
92
            $entityform = substr($pannelloamministrazioneentity, $strposEntity + 1);
93
            $projectname = substr($pannelloamministrazioneentity, 0, $strposEntity);
94
        } else {
95 1
            $entityform = $pannelloamministrazioneentity;
96 1
            $projectname = "";
97
        }
98 1
        $resultchk = $this->checkFormCrud($entityform, $projectname, $isAPI);
99
100 1
        if (0 !== $resultchk['errcode']) {
101
            return $resultchk;
102
        }
103 1
        $formcrudparms = array('entityform' => $entityform, '--generatemplate' => $generatemplate);
104 1
        if ($isAPI) {
105
            $formcrudparms['--isApi'] = true;
106
            $formcrudparms['--projectname'] = $projectname;
107
        }
108
109 1
        $retmsggenerateform = $this->pammutils->runSymfonyCommand('pannelloamministrazione:generateformcrud', $formcrudparms);
110
111 1
        $retmsg = array(
112 1
            'errcode' => $retmsggenerateform['errcode'],
113 1
            'command' => $retmsggenerateform['command'],
114 1
            'message' => $retmsggenerateform['message'],
115 1
        );
116
117 1
        return $retmsg;
118
    }
119
120
    /**
121
     *
122
     * @return array<mixed>
123
     */
124 1
    public function checkFormCrud(string $entityform, string $projectname = "", bool $isAPI = false)
125
    {
126
        /* @var $fs Filesystem */
127 1
        $fs = new Filesystem();
128 1
        $srcPath = $this->apppaths->getSrcPath();
129 1
        $appPath = $srcPath;
130 1
        if (!is_writable($appPath)) {
131
            return array('errcode' => -1, 'message' => $appPath . ' non scrivibile');
132
        }
133
134 1
        if (!$isAPI) {
135
            //Look for Entities... but they should already exist...
136 1
            $entityPath = $appPath . '/Entity' . DIRECTORY_SEPARATOR . $entityform . '.php';
137 1
            if (!$fs->exists($entityPath)) {
138 1
                return array('errcode' => -1, 'message' => $entityPath . ' entity non trovata');
139
            }
140
        } else {
141
            $apiUtil = new ApiUtils();
142
            $modelClass = $apiUtil->getModelClass($projectname, $entityform);
143
            if (!class_exists($modelClass)) {
144
                return array('errcode' => -1, 'message' => $modelClass . ' model not found');
145
            }
146
        }
147
148 1
        $formPath = $appPath . '/Form/' . $entityform . 'Type.php';
149 1
        if ($fs->exists($formPath)) {
150
            return array('errcode' => -1, 'message' => $formPath . ' esistente');
151
        }
152
153 1
        $controllerPath = $appPath . '/Controller' . DIRECTORY_SEPARATOR . $entityform . 'Controller.php';
154
155 1
        if ($fs->exists($controllerPath)) {
156
            return array('errcode' => -1, 'message' => $controllerPath . ' esistente');
157
        }
158
159 1
        $viewPathSrc = $this->apppaths->getTemplatePath() . DIRECTORY_SEPARATOR . $entityform;
160
161 1
        if ($fs->exists($viewPathSrc)) {
162
            return array('errcode' => -1, 'message' => $viewPathSrc . ' esistente');
163
        }
164
165 1
        return array('errcode' => 0, 'message' => 'OK');
166
    }
167
168
    /**
169
     *
170
     * @return array<mixed>
171
     */
172
    public function clearcache()
173
    {
174
        $cmdoutput = '';
175
        //$envs = array('dev', 'test', 'prod');
176
        $env = getenv("APP_ENV");
177
        if ($env === false) {
178
            throw new Exception("APP_ENV non definita");
179
        }
180
        $result = $this->pammutils->clearcache($env);
181
        $cmdoutput = $cmdoutput . $result['message'];
182
        if (0 !== $result['errcode']) {
183
            return $result;
184
        }
185
        $result['message'] = $cmdoutput;
186
187
        return $result;
188
    }
189
190
    /**
191
     *
192
     * @return array<mixed>
193
     */
194 1
    public function aggiornaSchemaDatabase(): array
195
    {
196 1
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
197
198 1
        return $result;
199
    }
200
}
201