Completed
Push — develop ( 7653f6...ec1821 )
by Andrea
15:53
created

GeneratorHelper::getDestinationEntityOrmPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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