Passed
Push — master ( ab984d...5ccca1 )
by Andrea
18:35 queued 20s
created

GeneratorHelper::removeExportJsonFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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