Passed
Push — master ( 38225e...e98f88 )
by Andrea
10:10
created

GeneratorHelper   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Test Coverage

Coverage 72.29%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 21
eloc 105
c 4
b 2
f 0
dl 0
loc 176
ccs 60
cts 83
cp 0.7229
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDestinationEntityYmlPath() 0 3 1
A getScriptGenerator() 0 10 3
A getExportJsonFile() 0 10 2
B checktables() 0 46 7
A checkprerequisiti() 0 49 5
A getJsonMwbGenerator() 0 28 1
A __construct() 0 4 1
A removeExportJsonFile() 0 5 1
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\DependencyInjection;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Finder\Finder;
7
use MwbExporter\Model\Table;
8
9
class GeneratorHelper
10
{
11
12
    private $container;
13
    private $apppaths;
14
15 2
    public function __construct($container)
16
    {
17 2
        $this->container = $container;
18 2
        $this->apppaths = $container->get("pannelloamministrazione.projectpath");
19 2
    }
20
21 2
    public function getDestinationEntityYmlPath()
22
    {
23 2
        return str_replace('/', "\/", str_replace('\\', '/', realpath($this->apppaths->getSrcPath() . '/../src/Entity/')));
24
    }
25
26 2
    public function checktables($destinationPath, $wbFile, $output)
27
    {
28 2
        $finder = new Finder();
29 2
        $fs = new Filesystem();
30
31 2
        $pathdoctrineyml = $destinationPath;
32
33
        //Si converte il nome file tabella.orm.yml se ha undercore
34 2
        $finder->in($pathdoctrineyml)->files()->name('*_*');
35 2
        $table = new Table();
36
37 2
        foreach ($finder as $file) {
38
            $oldfilename = $file->getPathName();
39
            $newfilename = $pathdoctrineyml . DIRECTORY_SEPARATOR . $table->beautify($file->getFileName());
40
            $fs->rename($oldfilename, $newfilename, true);
41
        }
42
43
        //Si cercano file con nomi errati
44 2
        $finderwrong = new Finder();
45 2
        $finderwrong->in($pathdoctrineyml)->files()->name('*_*');
46 2
        $wrongfilename = array();
47 2
        if (count($finderwrong) > 0) {
48
            foreach ($finderwrong as $file) {
49
                $wrongfilename[] = $file->getFileName();
50
                $fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName());
51
            }
52
        }
53 2
        $finderwrongcapitalize = new Finder();
54 2
        $finderwrongcapitalize->in($pathdoctrineyml)->files()->name('*.yml');
55 2
        foreach ($finderwrongcapitalize as $file) {
56
            if (!ctype_upper(substr($file->getFileName(), 0, 1))) {
57
                $wrongfilename[] = $file->getFileName();
58
                $fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName());
59
            }
60
        }
61
62 2
        if (count($wrongfilename) > 0) {
63
            $errout = '<error>Ci sono tabelle nel file ' . $wbFile . ' con nomi non consentiti:' .
64
                    implode(',', $wrongfilename) .
65
                    '. I nomi tabella devono essere : con la prima lettera maiuscola,underscore ammesso,doppio underscore non ammesso</error>';
66
67
            $output->writeln($errout);
68
69
            return -1;
70
        } else {
71 2
            return 0;
72
        }
73
    }
74
75 2
    public function checkprerequisiti($mwbfile, $output)
76
    {
77 2
        $fs = new Filesystem();
78
79 2
        $wbFile = $this->apppaths->getDocPath() . DIRECTORY_SEPARATOR . $mwbfile;
80 2
        $bundlePath = $this->apppaths->getSrcPath();
81
82
        $viewsPath = $bundlePath .
83 2
                DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
84
        $entityPath = $bundlePath .
85 2
                DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR;
86
        $formPath = $bundlePath .
87 2
                DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR;
88
89 2
        $scriptGenerator = $this->getScriptGenerator();
90
91 2
        $destinationPath = $this->getDestinationEntityYmlPath();
92 2
        $output->writeln('Creazione entities yml in ' . $destinationPath . ' da file ' . $mwbfile);
93
94 2
        if (!$fs->exists($bundlePath)) {
95
            $output->writeln('<error>Non esiste la cartella del bundle ' . $bundlePath . '</error>');
96
97
            return -1;
98
        }
99
100
        /* Creazione cartelle se non esistono nel bundle per l'esportazione */
101 2
        $fs->mkdir($destinationPath);
102 2
        $fs->mkdir($entityPath);
103 2
        $fs->mkdir($formPath);
104 2
        $fs->mkdir($viewsPath);
105
106 2
        if (!$fs->exists($wbFile)) {
107
            $output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!');
108
109
            return -1;
110
        }
111
112 2
        if (!$fs->exists($scriptGenerator)) {
113
            $output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>');
114
115
            return -1;
116
        }
117 2
        if (!$fs->exists($destinationPath)) {
118
            $output->writeln("<error>Non esiste la cartella per l'esportazione " . $destinationPath . ', controllare il nome del Bundle!</error>');
119
120
            return -1;
121
        }
122
123 2
        return 0;
124
    }
125
126 2
    public function getScriptGenerator()
127
    {
128 2
        $scriptGenerator = $this->apppaths->getBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
129 2
        if (!file_exists($scriptGenerator)) {
130 2
            $scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
131 2
            if (!file_exists($scriptGenerator)) {
132
                throw new \Exception("mysql-workbench-schema-export non trovato", -100);
133
            }
134
        }
135 2
        return $scriptGenerator;
136
    }
137
138 2
    public function getExportJsonFile()
139
    {
140 2
        $fs = new Filesystem();
141 2
        $cachedir = $this->apppaths->getCachePath();
142 2
        $exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json';
143 2
        if ($fs->exists($exportJson)) {
144 2
            $fs->remove($exportJson);
145
        }
146
147 2
        return $exportJson;
148
    }
149
150 2
    public function removeExportJsonFile()
151
    {
152 2
        $this->getExportJsonFile();
153
154 2
        return true;
155
    }
156
157 2
    public static function getJsonMwbGenerator()
158
    {
159
        $jsonTemplate = <<<EOF
160 2
{"export": "doctrine2-annotation",
161
    "zip": false,
162
    "dir": "[dir]",
163
    "params":
164
            {"indentation": 4,
165
                "useTabs": false, 
166
                "filename": "%entity%.%extension%",
167
                "skipPluralNameChecking": true,
168
                "backupExistingFile": false,
169
                "addGeneratorInfoAsComment": false,
170
                "useLoggedStorage": false,
171
                "enhanceManyToManyDetection": true,
172
                "logToConsole": false,
173
                "logFile": "",
174
                "bundleNamespace": "App",
175
                "entityNamespace": "Entity",
176
                "repositoryNamespace": "App\\\\Entity",
177
                "useAutomaticRepository": false,
178
                "generateExtendableEntity": true,
179
                "extendableEntityHasDiscriminator": false,
180
                "extendTableNameWithSchemaName": false
181
            }
182
}
183
EOF;
184 2
        return $jsonTemplate;
185
    }
186
}
187