These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 Symfony\Component\Console\Input\InputInterface; |
||
9 | use Symfony\Component\Console\Input\InputOption; |
||
10 | use Symfony\Component\Console\Output\OutputInterface; |
||
11 | use Throwable; |
||
12 | use function array_filter; |
||
13 | use function is_string; |
||
14 | use function sprintf; |
||
15 | use function ucfirst; |
||
16 | |||
17 | class DropCommand extends AbstractCommand |
||
18 | { |
||
19 | /** @var string[] */ |
||
20 | private $dropOrder = [self::INDEX, self::COLLECTION, self::DB]; |
||
21 | |||
22 | protected function configure() |
||
23 | { |
||
24 | $this |
||
25 | ->setName('odm:schema:drop') |
||
26 | ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
||
27 | ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Drop databases') |
||
28 | ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Drop collections') |
||
29 | ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Drop indexes') |
||
30 | ->setDescription('Drop databases, collections and indexes for your documents'); |
||
31 | } |
||
32 | |||
33 | protected function execute(InputInterface $input, OutputInterface $output) |
||
34 | { |
||
35 | $drop = array_filter($this->dropOrder, static function ($option) use ($input) { |
||
36 | return $input->getOption($option); |
||
37 | }); |
||
38 | |||
39 | // Default to the full drop order if no options were specified |
||
40 | $drop = empty($drop) ? $this->dropOrder : $drop; |
||
41 | |||
42 | $class = $input->getOption('class'); |
||
43 | $sm = $this->getSchemaManager(); |
||
44 | $isErrored = false; |
||
45 | |||
46 | View Code Duplication | foreach ($drop as $option) { |
|
0 ignored issues
–
show
|
|||
47 | try { |
||
48 | if (is_string($class)) { |
||
49 | $this->{'processDocument' . ucfirst($option)}($sm, $class); |
||
50 | } else { |
||
51 | $this->{'process' . ucfirst($option)}($sm); |
||
52 | } |
||
53 | $output->writeln(sprintf( |
||
54 | 'Dropped <comment>%s%s</comment> for <info>%s</info>', |
||
55 | $option, |
||
56 | is_string($class) ? ($option === self::INDEX ? '(es)' : '') : ($option === self::INDEX ? 'es' : 's'), |
||
57 | is_string($class) ? $class : 'all classes' |
||
58 | )); |
||
59 | } catch (Throwable $e) { |
||
60 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
61 | $isErrored = true; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | return $isErrored ? 255 : 0; |
||
66 | } |
||
67 | |||
68 | protected function processDocumentCollection(SchemaManager $sm, string $document) |
||
69 | { |
||
70 | $sm->dropDocumentCollection($document); |
||
71 | } |
||
72 | |||
73 | protected function processCollection(SchemaManager $sm) |
||
74 | { |
||
75 | $sm->dropCollections(); |
||
76 | } |
||
77 | |||
78 | protected function processDocumentDb(SchemaManager $sm, string $document) |
||
79 | { |
||
80 | $sm->dropDocumentDatabase($document); |
||
81 | } |
||
82 | |||
83 | protected function processDb(SchemaManager $sm) |
||
84 | { |
||
85 | $sm->dropDatabases(); |
||
86 | } |
||
87 | |||
88 | protected function processDocumentIndex(SchemaManager $sm, string $document) |
||
89 | { |
||
90 | $sm->deleteDocumentIndexes($document); |
||
91 | } |
||
92 | |||
93 | protected function processIndex(SchemaManager $sm) |
||
94 | { |
||
95 | $sm->deleteIndexes(); |
||
96 | } |
||
97 | } |
||
98 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.