Completed
Push — master ( e262bd...cc6d72 )
by Andrea
08:21
created

GeneratorHelper   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 70.97%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 4
dl 0
loc 190
ccs 66
cts 93
cp 0.7097
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getDestinationEntityYmlPath() 0 6 1
B checkprerequisiti() 0 51 5
A removeExportJsonFile() 0 6 1
B getJsonMwbGenerator() 0 25 1
C checktables() 0 48 7
B getScriptGenerator() 0 24 6
A getExportJsonFile() 0 11 2
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 1
    public function __construct($container)
16
    {
17 1
        $this->container = $container;
18 1
        $this->apppaths = new ProjectPath($container);
19 1
    }
20
21 1
    public function getDestinationEntityYmlPath($bundlePath)
22
    {
23 1
        return $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR .
24 1
                $bundlePath . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR .
25 1
                'config' . DIRECTORY_SEPARATOR . 'doctrine' . DIRECTORY_SEPARATOR;
26
    }
27
28 1
    public function checktables($destinationPath, $wbFile, $output)
29
    {
30 1
        $finder = new Finder();
31 1
        $fs = new Filesystem();
32
33 1
        $pathdoctrineyml = $destinationPath;
34
35
        //Si converte il nome file tabella.orm.yml se ha undercore
36 1
        $finder->in($pathdoctrineyml)->files()->name('*_*');
37 1
        $table = new Table();
38
39 1
        foreach ($finder as $file) {
40
            $oldfilename = $file->getPathName();
41
            $newfilename = $pathdoctrineyml . DIRECTORY_SEPARATOR . $table->beautify($file->getFileName());
42
            $fs->rename($oldfilename, $newfilename, true);
43
        }
44
45
        //Si cercano file con nomi errati
46 1
        $finderwrong = new Finder();
47 1
        $finderwrong->in($pathdoctrineyml)->files()->name('*_*');
48 1
        $wrongfilename = array();
49 1
        if (count($finderwrong) > 0) {
50
            foreach ($finderwrong as $file) {
51
                $wrongfilename[] = $file->getFileName();
52
                $fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName());
53
            }
54
        }
55 1
        $finderwrongcapitalize = new Finder();
56 1
        $finderwrongcapitalize->in($pathdoctrineyml)->files()->name('*.yml');
57 1
        foreach ($finderwrongcapitalize as $file) {
58 1
            if (!ctype_upper(substr($file->getFileName(), 0, 1))) {
59
                $wrongfilename[] = $file->getFileName();
60 1
                $fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName());
61
            }
62
        }
63
64 1
        if (count($wrongfilename) > 0) {
65
            $errout = '<error>Ci sono tabelle nel file ' . $wbFile . ' con nomi non consentiti:' .
66
                    implode(',', $wrongfilename) .
67
                    '. I nomi tabella devono essere : con la prima lettera maiuscola,underscore ammesso,doppio underscore non ammesso</error>';
68
69
            $output->writeln($errout);
70
71
            return -1;
72
        } else {
73 1
            return 0;
74
        }
75
    }
76
77 1
    public function checkprerequisiti($bundlename, $mwbfile, $output)
78
    {
79 1
        $fs = new Filesystem();
80
81 1
        $wbFile = $this->apppaths->getDocPath() . DIRECTORY_SEPARATOR . $mwbfile;
82 1
        $bundlePath = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename;
83
84
        $viewsPath = $bundlePath .
85 1
                DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
86
        $entityPath = $bundlePath .
87 1
                DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR;
88
        $formPath = $bundlePath .
89 1
                DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR;
90
91 1
        $scriptGenerator = $this->getScriptGenerator();
92
93 1
        $destinationPath = $this->getDestinationEntityYmlPath($bundlename);
94 1
        $output->writeln('Creazione entities yml in ' . $destinationPath . ' da file ' . $mwbfile);
95 1
        $destinationPath = $destinationPath . 'doctrine' . DIRECTORY_SEPARATOR;
96
97 1
        if (!$fs->exists($bundlePath)) {
98
            $output->writeln('<error>Non esiste la cartella del bundle ' . $bundlePath . '</error>');
99
100
            return -1;
101
        }
102
103
        /* Creazione cartelle se non esistono nel bundle per l'esportazione */
104 1
        $fs->mkdir($destinationPath);
105 1
        $fs->mkdir($entityPath);
106 1
        $fs->mkdir($formPath);
107 1
        $fs->mkdir($viewsPath);
108
109 1
        if (!$fs->exists($wbFile)) {
110
            $output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!');
111
112
            return -1;
113
        }
114
115 1
        if (!$fs->exists($scriptGenerator)) {
116
            $output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>');
117
118
            return -1;
119
        }
120 1
        if (!$fs->exists($destinationPath)) {
121
            $output->writeln("<error>Non esiste la cartella per l'esportazione " . $destinationPath . ', controllare il nome del Bundle!</error>');
122
123
            return -1;
124
        }
125
126 1
        return 0;
127
    }
128
129 1
    public function getScriptGenerator()
130
    {
131 1
        $scriptGenerator = "";
132 1
        if (version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '3.0') >= 0) {
133 1
            $scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
134
        } else {
135
            try {
136
                $scriptGenerator = $this->apppaths->getBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
137
            } catch (\Exception $exc) {
138
                //echo $exc->getTraceAsString();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
                if (!file_exists($scriptGenerator)) {
140
                    $scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
141
                }
142
            }
143
        }
144 1
        if (!file_exists($scriptGenerator)) {
145
            $scriptGenerator = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .
146
                    'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
147
        }
148 1
        if (!$scriptGenerator) {
149
            throw new \Exception("mysql-workbench-schema-export non trovato", -100);
150
        }
151 1
        return $scriptGenerator;
152
    }
153
154 1
    public function getExportJsonFile()
155
    {
156 1
        $fs = new Filesystem();
157 1
        $cachedir = $this->apppaths->getCachePath();
158 1
        $exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json';
159 1
        if ($fs->exists($exportJson)) {
160 1
            $fs->remove($exportJson);
161
        }
162
163 1
        return $exportJson;
164
    }
165
166 1
    public function removeExportJsonFile()
167
    {
168 1
        $this->getExportJsonFile();
169
170 1
        return true;
171
    }
172
173 1
    public static function getJsonMwbGenerator()
174
    {
175
        $jsonTemplate = <<<EOF
176 1
{"export": "doctrine2-yaml", 
177
  "zip": false, 
178
  "dir": "[dir]", 
179
  "params": 
180
    {"indentation": 4, 
181
    "useTabs": false, 
182
    "filename": "%table%.orm.%extension%", 
183
    "skipPluralNameChecking": true, 
184
    "backupExistingFile": false, 
185
    "addGeneratorInfoAsComment":false,
186
    "useLoggedStorage": false, 
187
    "enhanceManyToManyDetection": true, 
188
    "logToConsole": false, 
189
    "logFile": "", 
190
    "bundleNamespace": "[bundle]", 
191
    "entityNamespace": "Entity", 
192
    "repositoryNamespace": "%entity%", 
193
    "useAutomaticRepository": false, 
194
    "extendTableNameWithSchemaName": false}}
195
EOF;
196 1
        return $jsonTemplate;
197
    }
198
}
199