DropCommand::executeSchemaCommand()   B
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 63
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 38
nc 10
nop 5
dl 0
loc 63
ccs 0
cts 36
cp 0
crap 72
rs 8.0675
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools\Console\Command\SchemaTool;
6
7
use Doctrine\ORM\Tools\SchemaTool;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
use function count;
13
use function sprintf;
14
15
/**
16
 * Command to drop the database schema for a set of classes based on their mappings.
17
 */
18
class DropCommand extends AbstractCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function configure()
24
    {
25
        $this->setName('orm:schema-tool:drop')
26
             ->setDescription('Drop the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output')
27
             ->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Instead of trying to apply generated SQLs into EntityManager Storage Connection, output them.')
28
             ->addOption('force', 'f', InputOption::VALUE_NONE, "Don't ask for the deletion of the database, but force the operation to run.")
29
             ->addOption('full-database', null, InputOption::VALUE_NONE, 'Instead of using the Class Metadata to detect the database table schema, drop ALL assets that the database contains.')
30
             ->setHelp(<<<'EOT'
31
Processes the schema and either drop the database schema of EntityManager Storage Connection or generate the SQL output.
32
Beware that the complete database is dropped by this command, even tables that are not relevant to your metadata model.
33
34
<comment>Hint:</comment> If you have a database with tables that should not be managed
35
by the ORM, you can use a DBAL functionality to filter the tables and sequences down
36
on a global level:
37
38
    $config->setFilterSchemaAssetsExpression($regexp);
39
EOT
40
             );
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui)
47
    {
48
        $isFullDatabaseDrop = $input->getOption('full-database');
49
        $dumpSql            = $input->getOption('dump-sql') === true;
50
        $force              = $input->getOption('force') === true;
51
52
        if ($dumpSql) {
53
            if ($isFullDatabaseDrop) {
54
                $sqls = $schemaTool->getDropDatabaseSQL();
55
            } else {
56
                $sqls = $schemaTool->getDropSchemaSQL($metadatas);
57
            }
58
            $ui->text('The following SQL statements will be executed:');
59
            $ui->newLine();
60
61
            foreach ($sqls as $sql) {
62
                $ui->text(sprintf('    %s;', $sql));
63
            }
64
65
            return 0;
66
        }
67
68
        if ($force) {
69
            $ui->text('Dropping database schema...');
70
            $ui->newLine();
71
72
            if ($isFullDatabaseDrop) {
73
                $schemaTool->dropDatabase();
74
            } else {
75
                $schemaTool->dropSchema($metadatas);
76
            }
77
78
            $ui->success('Database schema dropped successfully!');
79
80
            return 0;
81
        }
82
83
        $ui->caution('This operation should not be executed in a production environment!');
84
85
        if ($isFullDatabaseDrop) {
86
            $sqls = $schemaTool->getDropDatabaseSQL();
87
        } else {
88
            $sqls = $schemaTool->getDropSchemaSQL($metadatas);
89
        }
90
91
        if (empty($sqls)) {
92
            $ui->success('Nothing to drop. The database is empty!');
93
94
            return 0;
95
        }
96
97
        $ui->text(
98
            [
99
                sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls)),
100
                '',
101
                'Please run the operation by passing one - or both - of the following options:',
102
                '',
103
                sprintf('    <info>%s --force</info> to execute the command', $this->getName()),
104
                sprintf('    <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()),
105
            ]
106
        );
107
108
        return 1;
109
    }
110
}
111