Passed
Push — master ( 4b682e...929ece )
by Andrea
18:49
created

GeneratorHelper::checkprerequisiti()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 0
cts 28
cp 0
rs 8.6315
c 0
b 0
f 0
cc 5
eloc 30
nc 5
nop 3
crap 30
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
    public function __construct($container)
16
    {
17
        $this->container = $container;
18
        $this->apppaths = $container->get("pannelloamministrazione.projectpath");
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal pannelloamministrazione.projectpath does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
19
    }
20
21
    public function getDestinationEntityYmlPath($bundlePath)
22
    {
23
        return $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR .
24
                $bundlePath . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR .
25
                'config' . DIRECTORY_SEPARATOR . 'doctrine' . DIRECTORY_SEPARATOR;
26
    }
27
28
    public function checktables($destinationPath, $wbFile, $output)
29
    {
30
        $finder = new Finder();
31
        $fs = new Filesystem();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
32
33
        $pathdoctrineyml = $destinationPath;
34
35
        //Si converte il nome file tabella.orm.yml se ha undercore
36
        $finder->in($pathdoctrineyml)->files()->name('*_*');
37
        $table = new Table();
38
39
        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
        $finderwrong = new Finder();
47
        $finderwrong->in($pathdoctrineyml)->files()->name('*_*');
48
        $wrongfilename = array();
49
        if (count($finderwrong) > 0) {
50
            foreach ($finderwrong as $file) {
51
                $wrongfilename[] = $file->getFileName();
52
                $fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName());
53
            }
54
        }
55
        $finderwrongcapitalize = new Finder();
56
        $finderwrongcapitalize->in($pathdoctrineyml)->files()->name('*.yml');
57
        foreach ($finderwrongcapitalize as $file) {
58
            if (!ctype_upper(substr($file->getFileName(), 0, 1))) {
59
                $wrongfilename[] = $file->getFileName();
60
                $fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName());
61
            }
62
        }
63
64
        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
            return 0;
74
        }
75
    }
76
77
    public function checkprerequisiti($bundlename, $mwbfile, $output)
78
    {
79
        $fs = new Filesystem();
80
81
        $wbFile = $this->apppaths->getDocPath() . DIRECTORY_SEPARATOR . $mwbfile;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
82
        $bundlePath = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename;
83
84
        $viewsPath = $bundlePath .
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
85
                DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
86
        $entityPath = $bundlePath .
87
                DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR;
88
        $formPath = $bundlePath .
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
89
                DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR;
90
91
        $scriptGenerator = $this->getScriptGenerator();
92
93
        $destinationPath = $this->getDestinationEntityYmlPath($bundlename);
94
        $output->writeln('Creazione entities yml in ' . $destinationPath . ' da file ' . $mwbfile);
95
        $destinationPath = $destinationPath . 'doctrine' . DIRECTORY_SEPARATOR;
96
97
        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
        $fs->mkdir($destinationPath);
105
        $fs->mkdir($entityPath);
106
        $fs->mkdir($formPath);
107
        $fs->mkdir($viewsPath);
108
109
        if (!$fs->exists($wbFile)) {
110
            $output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!');
111
112
            return -1;
113
        }
114
115
        if (!$fs->exists($scriptGenerator)) {
116
            $output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>');
117
118
            return -1;
119
        }
120
        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
        return 0;
127
    }
128
129
    public function getScriptGenerator()
130
    {
131
        $scriptGenerator = "";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
132
        if (version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '3.0') >= 0) {
133
            $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
        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
        if (!$scriptGenerator) {
149
            throw new \Exception("mysql-workbench-schema-export non trovato", -100);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal mysql-workbench-schema-export non trovato does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
150
        }
151
        return $scriptGenerator;
152
    }
153
154
    public function getExportJsonFile()
155
    {
156
        $fs = new Filesystem();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
157
        $cachedir = $this->apppaths->getCachePath();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
158
        $exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json';
159
        if ($fs->exists($exportJson)) {
160
            $fs->remove($exportJson);
161
        }
162
163
        return $exportJson;
164
    }
165
166
    public function removeExportJsonFile()
167
    {
168
        $this->getExportJsonFile();
169
170
        return true;
171
    }
172
173
    public static function getJsonMwbGenerator()
174
    {
175
        $jsonTemplate = <<<EOF
176
{"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
        return $jsonTemplate;
197
    }
198
}
199