Passed
Push — master ( 88d705...44ae42 )
by Andrea
12:16
created

Fifree2droptablesCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 3
    protected function configure()
14
    {
15 3
        $this
16 3
                ->setName('fifree2:droptables')
17 3
                ->setDescription('Eliminazione di tutte le tabelle fifree2')
18 3
                ->setHelp('ATTENZIONE, questo comando cancellerà tutte le informazioni presenti nel database!!')
19 3
                ->addOption('force', null, InputOption::VALUE_NONE, 'Se non impostato, il comando non avrà effetto');
20 3
    }
21
22
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        /* @var $em \Doctrine\ORM\EntityManager */
25
        $em = $this->getContainer()->get('doctrine')->getManager();
26
        $driver = $em->getConnection()->getDriver()->getName();
27
28
        if ($driver == "pdo_sqlite") {
29
            $output->writeln("Non previsto per driver: " . $driver);
30
            return 1;
31
        }
32
33
        $force = $input->getOption('force');
34
35
        if (!$force) {
36
            $output->writeln("Specificare l'opzione --force per eseguire il comando");
37
            return 1;
38
        }
39
        $addcascade = "";
40
        if ($driver == "pdo_pgsql") {
41
            $addcascade = " CASCADE";
42
        }
43
44
        //Truncate tabelle
45
        $tables = $em->getConnection()->getSchemaManager()->listTables();
46
        foreach ($tables as $table) {
47
            $tableName = $table->getName();
48
            $em->getConnection()->executeQuery(sprintf('TRUNCATE TABLE %s' . $addcascade, $tableName));
49
        }
50
        //Cancellazione tabelle
51
        foreach ($tables as $table) {
52
            $tableName = $table->getName();
53
            $em->getConnection()->executeQuery(sprintf('DROP TABLE %s' . $addcascade, $tableName));
54
        }
55
        if ($driver == "pdo_pgsql") {
56
            //Cancellazione sequences
57
            $sequences = $em->getConnection()->getSchemaManager()->listSequences();
58
            foreach ($sequences as $sequence) {
59
                $sequenceName = $sequence->getName();
60
                $em->getConnection()->executeQuery(sprintf('DROP SEQUENCE %s', $sequenceName));
61
            }
62
        }
63
    }
64
}
65