BiCoreBundleDroptablesCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 73.52%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 65
ccs 25
cts 34
cp 0.7352
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 43 6
A configure() 0 6 1
1
<?php
2
3
namespace Cdf\BiCoreBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Doctrine\ORM\EntityManagerInterface;
10
11
class BiCoreBundleDroptablesCommand extends Command
12
{
13
14
    protected static $defaultName = 'bicorebundle:droptables';
15
    private EntityManagerInterface $em;
16
17 1
    protected function configure(): void
18
    {
19 1
        $this
20 1
                ->setDescription('Eliminazione di tutte le tabelle bicorebundle')
21 1
                ->setHelp('ATTENZIONE, questo comando cancellerà tutte le informazioni presenti nel database!!')
22 1
                ->addOption('force', null, InputOption::VALUE_NONE, 'Se non impostato, il comando non avrà effetto');
23
    }
24
25 1
    public function __construct(EntityManagerInterface $em)
26
    {
27 1
        $this->em = $em;
28
29
        // you *must* call the parent constructor
30 1
        parent::__construct();
31
    }
32
33 1
    protected function execute(InputInterface $input, OutputInterface $output): int
34
    {
35
        /* @var $em \Doctrine\ORM\EntityManager */
36 1
        $em = $this->em;
37 1
        $driver = $em->getConnection()->getDatabasePlatform()->getName();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...ractPlatform::getName() has been deprecated: Identify platforms by their class. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

37
        $driver = /** @scrutinizer ignore-deprecated */ $em->getConnection()->getDatabasePlatform()->getName();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
38
39 1
        $force = $input->getOption('force');
40
41 1
        if (!$force) {
42
            $output->writeln("Specificare l'opzione --force per eseguire il comando");
43
44
            return 1;
45
        }
46
47
        //Truncate tabelle
48 1
        $tables = $em->getConnection()->getSchemaManager()->listTables();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

48
        $tables = /** @scrutinizer ignore-deprecated */ $em->getConnection()->getSchemaManager()->listTables();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
49
        //Cancellazione tabelle
50 1
        foreach ($tables as $table) {
51 1
            $tableName = $table->getName();
52
53
            switch ($driver) {
54 1
                case 'postgresql':
55 1
                    $em->getConnection()->executeQuery(sprintf('DROP TABLE %s CASCADE', $tableName));
56 1
                    $sequences = $em->getConnection()->getSchemaManager()->listSequences();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

56
                    $sequences = /** @scrutinizer ignore-deprecated */ $em->getConnection()->getSchemaManager()->listSequences();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
57 1
                    foreach ($sequences as $sequence) {
58 1
                        $sequenceName = $sequence->getName();
59 1
                        $em->getConnection()->executeQuery(sprintf('DROP SEQUENCE %s', $sequenceName));
60
                    }
61 1
                    break;
62
                case 'mysql':
63
                    $em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=0');
64
                    $em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName));
65
                    $em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=1');
66
                    break;
67
                default:
68
                    //$em->getConnection()->executeQuery(sprintf('DELETE FROM %s', $tableName));
69
                    $em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName));
70
                    break;
71
            }
72
        }
73
74 1
        $output->writeln('Done!');
75 1
        return 0;
76
    }
77
}
78