Passed
Push — master ( 98954f...37aea2 )
by Andrea
16:13 queued 12s
created

GeneratorHelper::getScriptGenerator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 6
c 1
b 1
f 0
nc 3
nop 0
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
1
<?php
2
3
namespace Cdf\PannelloAmministrazioneBundle\Utils;
4
5
use Cdf\PannelloAmministrazioneBundle\Utils\ProjectPath;
6
use Exception;
7
use ReflectionClass;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use function count;
12
13
class GeneratorHelper
14
{
15
16
    private ProjectPath $apppaths;
17
18 1
    public function __construct(ProjectPath $projectpath)
19
    {
20 1
        $this->apppaths = $projectpath;
21 1
    }
22
23 1
    public function getDestinationEntityOrmPath(): string
24
    {
25 1
        $path = $this->apppaths->getSrcPath() . '/../src/Entity/';
26 1
        $entitypath = realpath($path);
27 1
        if ($entitypath === false) {
28
            throw new Exception("Impossibile trovare il percorso " . $path);
29
        }
30 1
        if (DIRECTORY_SEPARATOR == '/') {
31 1
            return $entitypath;
32
        } else {
33
            return str_replace('\\', '\\\\', $entitypath);
34
        }
35
    }
36
37 1
    public function checktables(string $destinationPath, string $wbFile, OutputInterface $output): int
38
    {
39 1
        $fs = new Filesystem();
40
41 1
        $pathdoctrineorm = $destinationPath;
42
43
        //Si cercano file con nomi errati
44 1
        $finderwrong = new Finder();
45 1
        $finderwrong->in($pathdoctrineorm)->files()->name('*_*');
46 1
        $wrongfilename = array();
47 1
        if (count($finderwrong) > 0) {
48
            foreach ($finderwrong as $file) {
49
                $wrongfilename[] = $file->getFileName();
50
                $fs->remove($pathdoctrineorm . DIRECTORY_SEPARATOR . $file->getFileName());
51
            }
52
        }
53
54
        //Si cercano file con nomi campo errati
55 1
        $finderwrongproperty = new Finder();
56 1
        $finderwrongproperty->in($pathdoctrineorm)->files()->name('Base*.php');
57 1
        $wrongpropertyname = array();
58 1
        foreach ($finderwrongproperty as $file) {
59
            /** @phpstan-ignore-next-line */
60 1
            $ref = new ReflectionClass('App\\Entity\\' . basename($file->getFileName(), '.php'));
61 1
            $props = $ref->getProperties();
62 1
            foreach ($props as $prop) {
63 1
                $f = $prop->getName();
64 1
                if ($f !== strtolower($f) && false === strpos($f, 'RelatedBy')) {
65 1
                    $wrongpropertyname[] = $file->getFileName();
66 1
                    $fullpathprmbase = $pathdoctrineorm . DIRECTORY_SEPARATOR . $file->getFileName();
67 1
                    $fullpathprm = $pathdoctrineorm . DIRECTORY_SEPARATOR . substr($file->getFileName(), 4);
68
                    //$fs->remove($pathdoctrineorm . DIRECTORY_SEPARATOR . $file->getFileName());
69 1
                    $fs->rename($fullpathprmbase, $fullpathprmbase . '.ko', true);
70 1
                    $fs->rename($fullpathprm, $fullpathprm . '.ko', true);
71
                }
72
            }
73
        }
74
75 1
        if (count($wrongpropertyname) > 0) {
76 1
            $errout = '<error>CI SONO CAMPI NEL FILE ' . $wbFile . ' CON NOMI NON CONSENTITI: ' .
77 1
                    implode(',', $wrongpropertyname) .
78 1
                    '. I NOMI DEI CAMPI DEVONO ESSERE MINUSCOLI!</error>';
79
80 1
            $output->writeln($errout);
81
82 1
            return -1;
83
        } else {
84 1
            return 0;
85
        }
86
    }
87
88 1
    public function checkprerequisiti(string $mwbfile, OutputInterface $output): int
89
    {
90 1
        $fs = new Filesystem();
91
92 1
        $wbFile = $this->apppaths->getDocPath() . DIRECTORY_SEPARATOR . $mwbfile;
93 1
        $bundlePath = $this->apppaths->getSrcPath();
94
95
        $viewsPath = $bundlePath .
96 1
                DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
97
        $entityPath = $bundlePath .
98 1
                DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR;
99
        $formPath = $bundlePath .
100 1
                DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR;
101
102 1
        $scriptGenerator = $this->getScriptGenerator();
103
104 1
        $destinationPath = $this->getDestinationEntityOrmPath();
105 1
        $output->writeln('Creazione orm entities in ' . $destinationPath . ' da file ' . $mwbfile);
106
107 1
        if (!$fs->exists($bundlePath)) {
108
            $output->writeln('<error>Non esiste la cartella del bundle ' . $bundlePath . '</error>');
109
110
            return -1;
111
        }
112
113
        /* Creazione cartelle se non esistono nel bundle per l'esportazione */
114 1
        $fs->mkdir($destinationPath);
115 1
        $fs->mkdir($entityPath);
116 1
        $fs->mkdir($formPath);
117 1
        $fs->mkdir($viewsPath);
118
119 1
        if (!$fs->exists($wbFile)) {
120
            $output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!');
121
122
            return -1;
123
        }
124
125 1
        if (!$fs->exists($scriptGenerator)) {
126
            $output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>');
127
128
            return -1;
129
        }
130 1
        if (!$fs->exists($destinationPath)) {
131
            $output->writeln("<error>Non esiste la cartella per l'esportazione " . $destinationPath . ', controllare il nome del Bundle!</error>');
132
133
            return -1;
134
        }
135
136 1
        return 0;
137
    }
138
139 1
    public function getScriptGenerator(): string
140
    {
141 1
        $scriptGenerator = $this->apppaths->getBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
142 1
        if (!file_exists($scriptGenerator)) {
143 1
            $scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
144 1
            if (!file_exists($scriptGenerator)) {
145
                throw new Exception("mysql-workbench-schema-export non trovato", -100);
146
            }
147
        }
148 1
        return $scriptGenerator;
149
    }
150
151 1
    public function getExportJsonFile(): string
152
    {
153 1
        $fs = new Filesystem();
154 1
        $cachedir = $this->apppaths->getCachePath();
155 1
        $exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json';
156 1
        if ($fs->exists($exportJson)) {
157 1
            $fs->remove($exportJson);
158
        }
159
160 1
        return $exportJson;
161
    }
162
163 1
    public function removeExportJsonFile(): bool
164
    {
165 1
        $this->getExportJsonFile();
166
167 1
        return true;
168
    }
169
170 1
    public static function getJsonMwbGenerator(): string
171
    {
172
        $jsonTemplate = <<<EOF
173 1
{"export": "doctrine2-annotation",
174
    "zip": false,
175
    "dir": "[dir]",
176
    "params":
177
            {"indentation": 4,
178
                "useTabs": false, 
179
                "filename": "%entity%.%extension%",
180
                "skipPluralNameChecking": true,
181
                "backupExistingFile": false,
182
                "addGeneratorInfoAsComment": false,
183
                "useLoggedStorage": false,
184
                "enhanceManyToManyDetection": true,
185
                "logToConsole": false,
186
                "logFile": "",
187
                "bundleNamespace": "App",
188
                "entityNamespace": "Entity",
189
                "repositoryNamespace": "App\\\\Entity",
190
                "useAutomaticRepository": false,
191
                "generateExtendableEntity": true,
192
                "extendableEntityHasDiscriminator": false,
193
                "extendTableNameWithSchemaName": false
194
            }
195
}
196
EOF;
197
198 1
        return $jsonTemplate;
199
    }
200
}
201