Passed
Push — master ( d11a3f...d19abb )
by Andrea
17:59 queued 10s
created

GeneratorHelper::getScriptGenerator()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 13
ccs 5
cts 8
cp 0.625
crap 3.4746
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
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('*');
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
                    $fs->remove($pathdoctrineorm . DIRECTORY_SEPARATOR . $file->getFileName());
58
                }
59
            }
60
        }
61
62 1
        if (count($wrongpropertyname) > 0) {
63 1
            $errout = '<error>Ci sono campi nel file ' . $wbFile . ' con nomi non consentiti:' .
64 1
                    implode(',', $wrongpropertyname) .
65 1
                    '. I nomi dei campi devono essere lower case</error>';
66
67 1
            $output->writeln($errout);
68
69 1
            return -1;
70
        } else {
71 1
            return 0;
72
        }
73
    }
74
75 1
    public function checkprerequisiti($mwbfile, $output)
76
    {
77 1
        $fs = new Filesystem();
78
79 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...
80 1
        $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 1
                DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
84
        $entityPath = $bundlePath .
85 1
                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 1
                DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR;
88
89 1
        $scriptGenerator = $this->getScriptGenerator();
90
91 1
        $destinationPath = $this->getDestinationEntityOrmPath();
92 1
        $output->writeln('Creazione orm entities in ' . $destinationPath . ' da file ' . $mwbfile);
93
94 1
        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 1
        $fs->mkdir($destinationPath);
102 1
        $fs->mkdir($entityPath);
103 1
        $fs->mkdir($formPath);
104 1
        $fs->mkdir($viewsPath);
105
106 1
        if (!$fs->exists($wbFile)) {
107
            $output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!');
108
109
            return -1;
110
        }
111
112 1
        if (!$fs->exists($scriptGenerator)) {
113
            $output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>');
114
115
            return -1;
116
        }
117 1
        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 1
        return 0;
124
    }
125
126 1
    public function getScriptGenerator()
127
    {
128
        try {
129 1
            $bindir = $this->apppaths->getVendorBinPath();
130
        } catch (\Exception $exc) {
131
            $bindir = $this->apppaths->getBinPath();
132
        }
133 1
        $scriptGenerator = $bindir . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
134 1
        if (!file_exists($scriptGenerator)) {
135
            throw new \Exception('mysql-workbench-schema-export non trovato', -100);
136
        }
137
138 1
        return $scriptGenerator;
139
    }
140
141 1
    public function getExportJsonFile()
142
    {
143 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...
144 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...
145 1
        $exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json';
146 1
        if ($fs->exists($exportJson)) {
147 1
            $fs->remove($exportJson);
148
        }
149
150 1
        return $exportJson;
151
    }
152
153 1
    public function removeExportJsonFile()
154
    {
155 1
        $this->getExportJsonFile();
156
157 1
        return true;
158
    }
159
160 1
    public static function getJsonMwbGenerator()
161
    {
162
        $jsonTemplate = <<<EOF
163 1
{"export": "doctrine2-annotation",
164
    "zip": false,
165
    "dir": "[dir]",
166
    "params":
167
            {"indentation": 4,
168
                "useTabs": false, 
169
                "filename": "%entity%.%extension%",
170
                "skipPluralNameChecking": true,
171
                "backupExistingFile": false,
172
                "addGeneratorInfoAsComment": false,
173
                "useLoggedStorage": false,
174
                "enhanceManyToManyDetection": true,
175
                "logToConsole": false,
176
                "logFile": "",
177
                "bundleNamespace": "App",
178
                "entityNamespace": "Entity",
179
                "repositoryNamespace": "App\\\\Entity",
180
                "useAutomaticRepository": false,
181
                "generateExtendableEntity": true,
182
                "extendableEntityHasDiscriminator": false,
183
                "extendTableNameWithSchemaName": false
184
            }
185
}
186
EOF;
187
188 1
        return $jsonTemplate;
189
    }
190
}
191