Passed
Push — master ( 5e8bf6...1d619d )
by Andrea
25:03
created

GeneratorHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\PannelloAmministrazioneBundle\Utils;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Finder\Finder;
7
use MwbExporter\Model\Table;
8
use Cdf\PannelloAmministrazioneBundle\Utils\ProjectPath;
9
10
class GeneratorHelper
11
{
12
13
    private $apppaths;
14
15 1
    public function __construct(ProjectPath $projectpath)
16
    {
17 1
        $this->apppaths = $projectpath;
18 1
    }
19
20 1
    public function getDestinationEntityOrmPath()
21
    {
22 1
        $entitypath = realpath($this->apppaths->getSrcPath() . '/../src/Entity/');
23 1
        if (DIRECTORY_SEPARATOR == '/') {
24 1
            return $entitypath;
25
        } else {
26
            return str_replace('/', "\/", $entitypath);
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...
27
        }
28
    }
29
30 1
    public function checktables($destinationPath, $wbFile, $output)
31
    {
32 1
        $finder = new Finder();
33 1
        $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...
34
35 1
        $pathdoctrineorm = $destinationPath;
36
37
        //Si converte il nome file per l'Orm della tabella se ha undercore
38 1
        $finder->in($pathdoctrineorm)->files()->name('*_*');
39 1
        $table = new Table();
40
41 1
        foreach ($finder as $file) {
42
            $oldfilename = $file->getPathName();
43
            $newfilename = $pathdoctrineorm . DIRECTORY_SEPARATOR . $table->beautify($file->getFileName());
44
            $fs->rename($oldfilename, $newfilename, true);
45
        }
46
47
        //Si cercano file con nomi errati
48 1
        $finderwrong = new Finder();
49 1
        $finderwrong->in($pathdoctrineorm)->files()->name('*_*');
50 1
        $wrongfilename = array();
51 1
        if (count($finderwrong) > 0) {
52
            foreach ($finderwrong as $file) {
53
                $wrongfilename[] = $file->getFileName();
54
                $fs->remove($pathdoctrineorm . DIRECTORY_SEPARATOR . $file->getFileName());
55
            }
56
        }
57
58
        //Si cercano file con nomi campo errati
59 1
        $finderwrongproperty = new Finder();
60 1
        $finderwrongproperty->in($pathdoctrineorm)->files()->name('*');
61 1
        $wrongpropertyname = array();
62 1
        foreach ($finderwrongproperty as $file) {
63 1
            $ref = new \ReflectionClass("App\\Entity\\" . basename($file->getFileName(), ".php"));
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...
Coding Style Comprehensibility introduced by
The string literal App\\Entity\\ 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...
Coding Style Comprehensibility introduced by
The string literal .php 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...
64 1
            $props = $ref->getProperties();
65 1
            foreach ($props as $prop) {
66 1
                $f = $prop->getName();
67 1
                if ($f !== strtolower($f)) {
68 1
                    $wrongpropertyname[] = $file->getFileName();
69 1
                    $fs->remove($pathdoctrineorm . DIRECTORY_SEPARATOR . $file->getFileName());
70
                }
71
            }
72
        }
73
74 1
        if (count($wrongpropertyname) > 0) {
75 1
            $errout = '<error>Ci sono campi nel file ' . $wbFile . ' con nomi non consentiti:' .
76 1
                    implode(',', $wrongpropertyname) .
77 1
                    '. I nomi dei campi devono essere lower case</error>';
78
79 1
            $output->writeln($errout);
80
81 1
            return -1;
82
        } else {
83 1
            return 0;
84
        }
85
    }
86
87 1
    public function checkprerequisiti($mwbfile, $output)
88
    {
89 1
        $fs = new Filesystem();
90
91 1
        $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...
92 1
        $bundlePath = $this->apppaths->getSrcPath();
93
94
        $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...
95 1
                DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
96
        $entityPath = $bundlePath .
97 1
                DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR;
98
        $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...
99 1
                DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR;
100
101 1
        $scriptGenerator = $this->getScriptGenerator();
102
103 1
        $destinationPath = $this->getDestinationEntityOrmPath();
104 1
        $output->writeln('Creazione orm entities in ' . $destinationPath . ' da file ' . $mwbfile);
105
106 1
        if (!$fs->exists($bundlePath)) {
107
            $output->writeln('<error>Non esiste la cartella del bundle ' . $bundlePath . '</error>');
108
109
            return -1;
110
        }
111
112
        /* Creazione cartelle se non esistono nel bundle per l'esportazione */
113 1
        $fs->mkdir($destinationPath);
114 1
        $fs->mkdir($entityPath);
115 1
        $fs->mkdir($formPath);
116 1
        $fs->mkdir($viewsPath);
117
118 1
        if (!$fs->exists($wbFile)) {
119
            $output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!');
120
121
            return -1;
122
        }
123
124 1
        if (!$fs->exists($scriptGenerator)) {
125
            $output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>');
126
127
            return -1;
128
        }
129 1
        if (!$fs->exists($destinationPath)) {
130
            $output->writeln("<error>Non esiste la cartella per l'esportazione " . $destinationPath . ', controllare il nome del Bundle!</error>');
131
132
            return -1;
133
        }
134
135 1
        return 0;
136
    }
137
138 1
    public function getScriptGenerator()
139
    {
140 1
        $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...
141 1
        $scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
142 1
        if (!file_exists($scriptGenerator)) {
143
            $scriptGenerator = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .
144
                    'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
145
        }
146 1
        if (!$scriptGenerator) {
147
            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...
148
        }
149 1
        return $scriptGenerator;
150
    }
151
152 1
    public function getExportJsonFile()
153
    {
154 1
        $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...
155 1
        $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...
156 1
        $exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json';
157 1
        if ($fs->exists($exportJson)) {
158 1
            $fs->remove($exportJson);
159
        }
160
161 1
        return $exportJson;
162
    }
163
164 1
    public function removeExportJsonFile()
165
    {
166 1
        $this->getExportJsonFile();
167
168 1
        return true;
169
    }
170
171 1
    public static function getJsonMwbGenerator()
172
    {
173
        $jsonTemplate = <<<EOF
174 1
{"export": "doctrine2-annotation",
175
    "zip": false,
176
    "dir": "[dir]",
177
    "params":
178
            {"indentation": 4,
179
                "useTabs": false, 
180
                "filename": "%entity%.%extension%",
181
                "skipPluralNameChecking": true,
182
                "backupExistingFile": false,
183
                "addGeneratorInfoAsComment": false,
184
                "useLoggedStorage": false,
185
                "enhanceManyToManyDetection": true,
186
                "logToConsole": false,
187
                "logFile": "",
188
                "bundleNamespace": "App",
189
                "entityNamespace": "Entity",
190
                "repositoryNamespace": "App\\\\Entity",
191
                "useAutomaticRepository": false,
192
                "generateExtendableEntity": true,
193
                "extendableEntityHasDiscriminator": false,
194
                "extendTableNameWithSchemaName": false
195
            }
196
}
197
EOF;
198 1
        return $jsonTemplate;
199
    }
200
}
201