Fifree2droptablesCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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