Completed
Push — master ( 634bb9...a5365b )
by Andrea
60:40 queued 56:03
created

GeneratorHelper::getExportJsonFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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