Completed
Push — master ( 6ee0b3...df437e )
by Andrea
26:26 queued 20:19
created

GeneratorHelper::getJsonMwbGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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