Passed
Push — master ( acbcc2...152ef5 )
by Andrea
17:16 queued 11s
created

Fifree2droptablesCommand::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.5384

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 26
c 2
b 2
f 0
dl 0
loc 40
ccs 14
cts 26
cp 0.5385
rs 8.8817
cc 6
nc 6
nop 2
crap 9.5384
1
<?php
2
3
namespace Fi\CoreBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class Fifree2droptablesCommand extends ContainerAwareCommand
11
{
12
13 6
    protected function configure()
14
    {
15
        $this
16 6
                ->setName('fifree2:droptables')
17 6
                ->setDescription('Eliminazione di tutte le tabelle fifree2')
18 6
                ->setHelp('ATTENZIONE, questo comando cancellerà tutte le informazioni presenti nel database!!')
19 6
                ->addOption('force', null, InputOption::VALUE_NONE, 'Se non impostato, il comando non avrà effetto');
20 6
    }
21
22 1
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
               /* @var $em \Doctrine\ORM\EntityManager */
25 1
        $em = $this->getContainer()->get('doctrine')->getManager();
26 1
        $driver = $em->getConnection()->getDatabasePlatform()->getName();
27
28 1
        $force = $input->getOption('force');
29
30 1
        if (!$force) {
31
            $output->writeln("Specificare l'opzione --force per eseguire il comando");
32
            return 1;
33
        }
34
35
        //Truncate tabelle
36 1
        $tables = $em->getConnection()->getSchemaManager()->listTables();
37
        //Cancellazione tabelle
38 1
        foreach ($tables as $table) {
39 1
            $tableName = $table->getName();
40 1
            switch ($driver) {
41 1
                case "postgresql":
42
                    $em->getConnection()->executeQuery(sprintf('DROP TABLE %s CASCADE', $tableName));
43
                    $sequences = $em->getConnection()->getSchemaManager()->listSequences();
44
                    foreach ($sequences as $sequence) {
45
                        $sequenceName = $sequence->getName();
46
                        $em->getConnection()->executeQuery(sprintf('DROP SEQUENCE %s', $sequenceName));
47
                    }
48
                    break;
49 1
                case "mysql":
50
                    $em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=0');
51
                    $em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName));
52
                    $em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=1');
53
                    break;
54
                default:
55
                    //$em->getConnection()->executeQuery(sprintf('DELETE FROM %s', $tableName));
56 1
                    $em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName));
57 1
                    break;
58
            }
59
        }
60
61 1
        $output->writeln("Done!");
62 1
    }
63
}
64