Completed
Push — master ( 60d200...e262bd )
by Andrea
08:44
created

GenerateymlentitiesCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Fi\OsBundle\DependencyInjection\OsFunctions;
10
use Fi\PannelloAmministrazioneBundle\DependencyInjection\ProjectPath;
11
use Fi\PannelloAmministrazioneBundle\DependencyInjection\PannelloAmministrazioneUtils;
12
use Fi\PannelloAmministrazioneBundle\DependencyInjection\GeneratorHelper;
13
14
class GenerateymlentitiesCommand extends ContainerAwareCommand
15
{
16
17
    protected $apppaths;
18
    protected $genhelper;
19
    protected $pammutils;
20
21 1
    protected function configure()
22
    {
23
        $this
24 1
                ->setName('pannelloamministrazione:generateymlentities')
25 1
                ->setDescription('Genera le entities partendo da un modello workbeanch mwb')
26 1
                ->setHelp('Genera i ifle yml per le entities partendo da un modello workbeanch mwb, <br/>fifree.mwb Fi/CoreBundle default<br/>')
27 1
                ->addArgument('mwbfile', InputArgument::REQUIRED, 'Nome file mwb, fifree.mwb')
28 1
                ->addArgument('bundlename', InputArgument::REQUIRED, 'Nome del bundle, Fi/CoreBundle')
29 1
                ->addArgument('em', InputArgument::OPTIONAL, 'Entity manager, default = default');
30 1
    }
31
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        set_time_limit(0);
35
        $this->apppaths = new ProjectPath($this->getContainer());
36
        $this->genhelper = new GeneratorHelper($this->getContainer());
37
        $this->pammutils = new PannelloAmministrazioneUtils($this->getContainer());
38
39
        $bundlename = $input->getArgument('bundlename');
40
        $mwbfile = $input->getArgument('mwbfile');
41
42
        if (!$input->getArgument('em')) {
43
            $emdest = 'default';
0 ignored issues
show
Unused Code introduced by
$emdest is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
        } else {
45
            $emdest = $input->getArgument('em');
0 ignored issues
show
Unused Code introduced by
$emdest is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46
        }
47
48
        $wbFile = $this->apppaths->getDocPath() . DIRECTORY_SEPARATOR . $mwbfile;
49
        $checkprerequisiti = $this->genhelper->checkprerequisiti($bundlename, $mwbfile, $output);
50
51
        if ($checkprerequisiti < 0) {
52
            return -1;
53
        }
54
55
        $destinationPath = $this->genhelper->getDestinationEntityYmlPath($bundlename);
56
57
        $command = $this->getExportJsonCommand($bundlename, $wbFile);
58
59
        $schemaupdateresult = PannelloAmministrazioneUtils::runCommand($command);
60
        if ($schemaupdateresult["errcode"] < 0) {
61
            $output->writeln($schemaupdateresult["errmsg"]);
62
            return 1;
63
        } else {
64
            $output->writeln($schemaupdateresult["errmsg"]);
65
        }
66
67
        $this->genhelper->removeExportJsonFile();
68
69
        $tablecheck = $this->genhelper->checktables($destinationPath, $wbFile, $output);
70
71
        if ($tablecheck < 0) {
72
            return 1;
73
        }
74
75
        $output->writeln('<info>Entities yml create</info>');
76
        /* $generateentitiesresult = $this->pammutils->clearCache();
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
77
          if ($generateentitiesresult["errcode"] < 0) {
78
          $output->writeln($generateentitiesresult["errmsg"]);
79
          return 1;
80
          } else {
81
          $output->writeln($generateentitiesresult["errmsg"]);
82
          } */
83
        return 0;
84
    }
85
86
    private function getExportJsonCommand($bundlePath, $wbFile)
87
    {
88
        $exportJson = $this->genhelper->getExportJsonFile();
89
        $scriptGenerator = $this->genhelper->getScriptGenerator();
90
        $destinationPathEscaped = str_replace('/', "\/", str_replace('\\', '/', $this->genhelper->getDestinationEntityYmlPath($bundlePath)));
91
        $bundlePathEscaped = str_replace('\\', '\\\\', str_replace('/', '\\', $bundlePath));
92
93
        $exportjsonfile = GeneratorHelper::getJsonMwbGenerator();
94
95
        $bundlejson = str_replace('[bundle]', str_replace('/', '', $bundlePathEscaped), $exportjsonfile);
96
        $exportjsonreplaced = str_replace('[dir]', $destinationPathEscaped, $bundlejson);
97
        file_put_contents($exportJson, $exportjsonreplaced);
98
        $sepchr = OsFunctions::getSeparator();
99
        if (OsFunctions::isWindows()) {
100
            $command = 'cd ' . $this->apppaths->getRootPath() . $sepchr
101
                    . $scriptGenerator . '.bat --export=doctrine2-yaml '
102
                    . ' --config=' .
103
                    $exportJson . ' ' . $wbFile . ' ' . $destinationPathEscaped;
104
        } else {
105
            $phpPath = OsFunctions::getPHPExecutableFromPath();
106
            $command = 'cd ' . $this->apppaths->getRootPath() . $sepchr
107
                    . $phpPath . ' ' . $scriptGenerator . ' --export=doctrine2-yaml '
108
                    . ' --config=' .
109
                    $exportJson . ' ' . $wbFile . ' ' . $destinationPathEscaped;
110
        }
111
112
        return $command;
113
    }
114
}
115