Passed
Push — master ( a74354...254a60 )
by Andrea
18:29 queued 11s
created

Commands::checkFormCrud()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.8033

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 8
eloc 23
nc 11
nop 3
dl 0
loc 41
ccs 16
cts 23
cp 0.6957
crap 9.8033
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
9
class Commands
10
{
11
    /* @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...
12
13
    private $apppaths;
14
    /* @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...
15
    private $pammutils;
16
17 1
    public function __construct(ProjectPath $projectpath, Utility $pautils)
18
    {
19 1
        $this->apppaths = $projectpath;
20 1
        $this->pammutils = $pautils;
21 1
    }
22
23
    // @codeCoverageIgnoreStart
24
    public function getVcs()
25
    {
26
        $fs = new Filesystem();
27
28
        $projectDir = $this->apppaths->getRootPath();
29
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.svn')) {
30
            $command = 'svn update';
31
        }
32
        if ($fs->exists($projectDir . DIRECTORY_SEPARATOR . '.git')) {
33
            $command = 'git pull';
34
        }
35
        if (!$command) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $command does not seem to be defined for all execution paths leading up to this point.
Loading history...
36
            throw new Exception('Vcs non trovato', 100);
37
        }
38
39
        return $this->pammutils->runCommand($command, $projectDir);
40
    }
41
42
    // @codeCoverageIgnoreEnd
43 1
    public function generateEntity($wbFile)
44
    {
45 1
        $command = 'pannelloamministrazione:generateormentities';
46 1
        $result = $this->pammutils->runSymfonyCommand($command, array('mwbfile' => $wbFile));
47
48 1
        if (0 != $result['errcode']) {
49
            return array(
50 1
                'errcode' => -1,
51 1
                'command' => $command,
52 1
                'message' => 'Errore nel comando:' . $command . ';' . $result['message'],
53
            );
54
        }
55
56
        return array(
57 1
            'errcode' => 0,
58 1
            'command' => $command,
59 1
            'message' => 'Eseguito comando:' . $command . ';' . $result['message'],);
60
    }
61
62 1
    public function generateFormCrud($entityform, $generatemplate, $isAPI = false)
63
    {
64
        // check if some item already exist, and it interrupts the execution if any
65 1
        $pannelloamministrazioneentity = $entityform;
66
        /* @var $fs Filesystem */
67 1
        if ($isAPI) {
68
            $entityform = substr($pannelloamministrazioneentity, strpos($pannelloamministrazioneentity, ".") + 1);
69
            $projectname = substr($pannelloamministrazioneentity, 0, strpos($pannelloamministrazioneentity, "."));
70
        } else {
71 1
            $entityform = $pannelloamministrazioneentity;
72 1
            $projectname = "";
73
        }
74 1
        $resultchk = $this->checkFormCrud($entityform, $projectname, $isAPI);
75
76 1
        if (0 !== $resultchk['errcode']) {
77
            return $resultchk;
78
        }
79 1
        $formcrudparms = array('entityform' => $entityform, '--generatemplate' => $generatemplate);
80 1
        if ($isAPI) {
81
            $formcrudparms['--isApi'] = true;
82
            $formcrudparms['--projectname'] = $projectname;
83
        }
84
85 1
        $retmsggenerateform = $this->pammutils->runSymfonyCommand('pannelloamministrazione:generateformcrud', $formcrudparms);
86
87
        $retmsg = array(
88 1
            'errcode' => $retmsggenerateform['errcode'],
89 1
            'command' => $retmsggenerateform['command'],
90 1
            'message' => $retmsggenerateform['message'],
91
        );
92
93 1
        return $retmsg;
94
    }
95
96 1
    public function checkFormCrud($entityform, string $projectname = "", bool $isAPI = false)
97
    {
98
        /* @var $fs Filesystem */
99 1
        $fs = new Filesystem();
100 1
        $srcPath = $this->apppaths->getSrcPath();
101 1
        $appPath = $srcPath;
102 1
        if (!is_writable($appPath)) {
103
            return array('errcode' => -1, 'message' => $appPath . ' non scrivibile');
104
        }
105
106 1
        if (!$isAPI) {
107
            //Look for Entities... but they should already exist...
108 1
            $entityPath = $appPath . '/Entity' . DIRECTORY_SEPARATOR . $entityform . '.php';
109 1
            if (!$fs->exists($entityPath)) {
110 1
                return array('errcode' => -1, 'message' => $entityPath . ' entity non trovata');
111
            }
112
        } else {
113
            $modelClass = ApiUtils::getModelClass($projectname, $entityform);
114
            if (!class_exists($modelClass)) {
115
                return array('errcode' => -1, 'message' => $modelClass . ' model not found');
116
            }
117
        }
118
119 1
        $formPath = $appPath . '/Form/' . $entityform . 'Type.php';
120 1
        if ($fs->exists($formPath)) {
121
            return array('errcode' => -1, 'message' => $formPath . ' esistente');
122
        }
123
124 1
        $controllerPath = $appPath . '/Controller' . DIRECTORY_SEPARATOR . $entityform . 'Controller.php';
125
126 1
        if ($fs->exists($controllerPath)) {
127
            return array('errcode' => -1, 'message' => $controllerPath . ' esistente');
128
        }
129
130 1
        $viewPathSrc = $this->apppaths->getTemplatePath() . DIRECTORY_SEPARATOR . $entityform;
131
132 1
        if ($fs->exists($viewPathSrc)) {
133
            return array('errcode' => -1, 'message' => $viewPathSrc . ' esistente');
134
        }
135
136 1
        return array('errcode' => 0, 'message' => 'OK');
137
    }
138
139
    public function clearcache()
140
    {
141
        $cmdoutput = '';
142
        //$envs = array('dev', 'test', 'prod');
143
        $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...
144
        foreach ($envs as $env) {
145
            $result = $this->pammutils->clearcache($env);
146
            $cmdoutput = $cmdoutput . $result['message'];
147
            if (0 !== $result['errcode']) {
148
                return $result;
149
            }
150
            $result['message'] = $cmdoutput;
151
        }
152
153
        return $result;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result seems to be defined by a foreach iteration on line 144. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
154
    }
155
156 1
    public function aggiornaSchemaDatabase()
157
    {
158 1
        $result = $this->pammutils->runSymfonyCommand('doctrine:schema:update', array('--force' => true));
159
160 1
        return $result;
161
    }
162
}
163