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

GeneratorHelper   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 84.88%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 23
eloc 107
c 1
b 1
f 0
dl 0
loc 180
ccs 73
cts 86
cp 0.8488
rs 10

8 Methods

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