DropCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;
6
7
use Doctrine\ODM\MongoDB\SchemaManager;
8
use MongoDB\Driver\WriteConcern;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Throwable;
13
use function array_filter;
14
use function is_string;
15
use function sprintf;
16
use function ucfirst;
17
18
class DropCommand extends AbstractCommand
19
{
20
    /** @var string[] */
21
    private $dropOrder = [self::INDEX, self::COLLECTION, self::DB];
22
23
    protected function configure()
24
    {
25
        parent::configure();
26
27
        $this
28
            ->setName('odm:schema:drop')
29
            ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)')
30
            ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Drop databases')
31
            ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Drop collections')
32
            ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Drop indexes')
33
            ->setDescription('Drop databases, collections and indexes for your documents');
34
    }
35
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $drop = array_filter($this->dropOrder, static function ($option) use ($input) {
39
            return $input->getOption($option);
40
        });
41
42
        // Default to the full drop order if no options were specified
43
        $drop = empty($drop) ? $this->dropOrder : $drop;
44
45
        $class     = $input->getOption('class');
46
        $sm        = $this->getSchemaManager();
47
        $isErrored = false;
48
49
        foreach ($drop as $option) {
50
            try {
51
                if (is_string($class)) {
52
                    $this->{'processDocument' . ucfirst($option)}($sm, $class, $this->getMaxTimeMsFromInput($input), $this->getWriteConcernFromInput($input));
53
                } else {
54
                    $this->{'process' . ucfirst($option)}($sm, $this->getMaxTimeMsFromInput($input), $this->getWriteConcernFromInput($input));
55
                }
56
                $output->writeln(sprintf(
57
                    'Dropped <comment>%s%s</comment> for <info>%s</info>',
58
                    $option,
59
                    is_string($class) ? ($option === self::INDEX ? '(es)' : '') : ($option === self::INDEX ? 'es' : 's'),
60
                    is_string($class) ? $class : 'all classes'
61
                ));
62
            } catch (Throwable $e) {
63
                $output->writeln('<error>' . $e->getMessage() . '</error>');
64
                $isErrored = true;
65
            }
66
        }
67
68
        return $isErrored ? 255 : 0;
69
    }
70
71
    protected function processDocumentCollection(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern)
72
    {
73
        $sm->dropDocumentCollection($document, $maxTimeMs, $writeConcern);
74
    }
75
76
    protected function processCollection(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern)
77
    {
78
        $sm->dropCollections($maxTimeMs, $writeConcern);
79
    }
80
81
    protected function processDocumentDb(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern)
82
    {
83
        $sm->dropDocumentDatabase($document, $maxTimeMs, $writeConcern);
84
    }
85
86
    protected function processDb(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern)
87
    {
88
        $sm->dropDatabases($maxTimeMs, $writeConcern);
89
    }
90
91
    protected function processDocumentIndex(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern)
92
    {
93
        $sm->deleteDocumentIndexes($document, $maxTimeMs, $writeConcern);
94
    }
95
96
    protected function processIndex(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern)
97
    {
98
        $sm->deleteIndexes($maxTimeMs, $writeConcern);
99
    }
100
}
101