Passed
Push — master ( 2a39ec...3b6c56 )
by Andrea
03:42
created

GeneratorHelper   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
wmc 21
eloc 107
dl 0
loc 178
ccs 60
cts 85
cp 0.7059
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDestinationEntityYmlPath() 0 3 1
A getScriptGenerator() 0 12 3
A getExportJsonFile() 0 10 2
B checktables() 0 46 7
A checkprerequisiti() 0 49 5
A getJsonMwbGenerator() 0 28 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");
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 2
    }
20
21 2
    public function getDestinationEntityYmlPath()
22
    {
23 2
        return str_replace('/', "\/", str_replace('\\', '/', realpath($this->apppaths->getSrcPath() . '/../src/Entity/')));
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...
24
    }
25
26 2
    public function checktables($destinationPath, $wbFile, $output)
27
    {
28 2
        $finder = new Finder();
29 2
        $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...
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;
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...
80 2
        $bundlePath = $this->apppaths->getSrcPath();
81
82
        $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...
83 2
                DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
84
        $entityPath = $bundlePath .
85 2
                DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR;
86
        $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...
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 = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $scriptGenerator is dead and can be removed.
Loading history...
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...
129 2
        $scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
130 2
        if (!file_exists($scriptGenerator)) {
131
            $scriptGenerator = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .
132
                    'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
133
        }
134 2
        if (!$scriptGenerator) {
135
            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...
136
        }
137 2
        return $scriptGenerator;
138
    }
139
140 2
    public function getExportJsonFile()
141
    {
142 2
        $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...
143 2
        $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...
144 2
        $exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json';
145 2
        if ($fs->exists($exportJson)) {
146 2
            $fs->remove($exportJson);
147
        }
148
149 2
        return $exportJson;
150
    }
151
152 2
    public function removeExportJsonFile()
153
    {
154 2
        $this->getExportJsonFile();
155
156 2
        return true;
157
    }
158
159 2
    public static function getJsonMwbGenerator()
160
    {
161
        $jsonTemplate = <<<EOF
162 2
{"export": "doctrine2-annotation",
163
    "zip": false,
164
    "dir": "[dir]",
165
    "params":
166
            {"indentation": 4,
167
                "useTabs": false, 
168
                "filename": "%entity%.%extension%",
169
                "skipPluralNameChecking": true,
170
                "backupExistingFile": false,
171
                "addGeneratorInfoAsComment": false,
172
                "useLoggedStorage": false,
173
                "enhanceManyToManyDetection": true,
174
                "logToConsole": false,
175
                "logFile": "",
176
                "bundleNamespace": "App",
177
                "entityNamespace": "Entity",
178
                "repositoryNamespace": "App\\\\Entity",
179
                "useAutomaticRepository": false,
180
                "generateExtendableEntity": true,
181
                "extendableEntityHasDiscriminator": false,
182
                "extendTableNameWithSchemaName": false
183
            }
184
}
185
EOF;
186 2
        return $jsonTemplate;
187
    }
188
}
189