Passed
Push — develop ( f2c7f2...ebab67 )
by Andrea
09:39 queued 12s
created

Commands::checkFormCrud()   B

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 4
Bugs 1 Features 0
Metric Value
cc 8
eloc 24
nc 11
nop 3
dl 0
loc 42
ccs 16
cts 24
cp 0.6667
crap 10.3696
rs 8.4444
c 4
b 1
f 0
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 1
    }
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
    
56
    public function generateEntity(string $wbFile) : array
57
    {
58
        $command = 'pannelloamministrazione:generateormentities';
59
        $result = $this->pammutils->runSymfonyCommand($command, array('mwbfile' => $wbFile));
60
61
        if (0 != $result['errcode']) {
62
            return array(
63
                'errcode' => -1,
64
                'command' => $command,
65
                'message' => 'Errore nel comando:' . $command . ';' . $result['message'],
66
            );
67
        }
68
69
        return array(
70
            'errcode' => 0,
71
            'command' => $command,
72
            'message' => 'Eseguito comando:' . $command . ';' . $result['message'],);
73
    }
74
75
    /**
76
     *
77
     * @return array<mixed>
78
     */
79 1
    public function generateFormCrud(string $entityform, bool $generatemplate, bool $isAPI = false)
80
    {
81
        // check if some item already exist, and it interrupts the execution if any
82 1
        $pannelloamministrazioneentity = $entityform;
83
        /* @var $fs Filesystem */
84 1
        if ($isAPI) {
85
            $entityform = substr($pannelloamministrazioneentity, strpos($pannelloamministrazioneentity, ".") + 1);
86
            $projectname = substr($pannelloamministrazioneentity, 0, strpos($pannelloamministrazioneentity, "."));
87
        } else {
88 1
            $entityform = $pannelloamministrazioneentity;
89 1
            $projectname = "";
90
        }
91 1
        $resultchk = $this->checkFormCrud($entityform, $projectname, $isAPI);
92
93 1
        if (0 !== $resultchk['errcode']) {
94
            return $resultchk;
95
        }
96 1
        $formcrudparms = array('entityform' => $entityform, '--generatemplate' => $generatemplate);
97 1
        if ($isAPI) {
98
            $formcrudparms['--isApi'] = true;
99
            $formcrudparms['--projectname'] = $projectname;
100
        }
101
102 1
        $retmsggenerateform = $this->pammutils->runSymfonyCommand('pannelloamministrazione:generateformcrud', $formcrudparms);
103
104
        $retmsg = array(
105 1
            'errcode' => $retmsggenerateform['errcode'],
106 1
            'command' => $retmsggenerateform['command'],
107 1
            'message' => $retmsggenerateform['message'],
108
        );
109
110 1
        return $retmsg;
111
    }
112
113
    /**
114
     *
115
     * @return array<mixed>
116
     */
117 1
    public function checkFormCrud(string $entityform, string $projectname = "", bool $isAPI = false)
118
    {
119
        /* @var $fs Filesystem */
120 1
        $fs = new Filesystem();
121 1
        $srcPath = $this->apppaths->getSrcPath();
122 1
        $appPath = $srcPath;
123 1
        if (!is_writable($appPath)) {
124
            return array('errcode' => -1, 'message' => $appPath . ' non scrivibile');
125
        }
126
127 1
        if (!$isAPI) {
128
            //Look for Entities... but they should already exist...
129 1
            $entityPath = $appPath . '/Entity' . DIRECTORY_SEPARATOR . $entityform . '.php';
130 1
            if (!$fs->exists($entityPath)) {
131 1
                return array('errcode' => -1, 'message' => $entityPath . ' entity non trovata');
132
            }
133
        } else {
134
            $apiUtil = new ApiUtils();
135
            $modelClass = $apiUtil->getModelClass($projectname, $entityform);
136
            if (!class_exists($modelClass)) {
137
                return array('errcode' => -1, 'message' => $modelClass . ' model not found');
138
            }
139
        }
140
141 1
        $formPath = $appPath . '/Form/' . $entityform . 'Type.php';
142 1
        if ($fs->exists($formPath)) {
143
            return array('errcode' => -1, 'message' => $formPath . ' esistente');
144
        }
145
146 1
        $controllerPath = $appPath . '/Controller' . DIRECTORY_SEPARATOR . $entityform . 'Controller.php';
147
148 1
        if ($fs->exists($controllerPath)) {
149
            return array('errcode' => -1, 'message' => $controllerPath . ' esistente');
150
        }
151
152 1
        $viewPathSrc = $this->apppaths->getTemplatePath() . DIRECTORY_SEPARATOR . $entityform;
153
154 1
        if ($fs->exists($viewPathSrc)) {
155
            return array('errcode' => -1, 'message' => $viewPathSrc . ' esistente');
156
        }
157
158 1
        return array('errcode' => 0, 'message' => 'OK');
159
    }
160
161
    /**
162
     *
163
     * @return array<mixed>
164
     */
165
    public function clearcache()
166
    {
167
        $cmdoutput = '';
168
        //$envs = array('dev', 'test', 'prod');
169
        $envs[] = getenv("APP_ENV");
0 ignored issues
show
Comprehensibility Best Practice introduced by
$envs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $envs = array(); before regardless.
Loading history...
170
        foreach ($envs as $env) {
171
            $result = $this->pammutils->clearcache($env);
172
            $cmdoutput = $cmdoutput . $result['message'];
173
            if (0 !== $result['errcode']) {
174
                return $result;
175
            }
176
            $result['message'] = $cmdoutput;
177
        }
178
179
        return $result;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be defined by a foreach iteration on line 170. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
180
    }
181
182
    /**
183
     *
184
     * @return array<mixed>
185
     */
186 1
    public function aggiornaSchemaDatabase(): array
187
    {
188 1
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
189
190 1
        return $result;
191
    }
192
}
193