Passed
Push — master ( 832cd5...6641eb )
by Andrea
21:59 queued 13s
created

Commands   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 67.14%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 81
dl 0
loc 154
rs 10
c 5
b 1
f 0
ccs 47
cts 70
cp 0.6714
wmc 23

7 Methods

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