Fifree2droptablesCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 7
eloc 32
c 2
b 2
f 0
dl 0
loc 52
ccs 20
cts 32
cp 0.625
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 40 6
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