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 BadMethodCallException; |
||
8 | use Doctrine\ODM\MongoDB\SchemaManager; |
||
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 is_string; |
||
14 | use function sprintf; |
||
15 | |||
16 | class UpdateCommand extends AbstractCommand |
||
17 | { |
||
18 | /** @var int|null */ |
||
19 | private $timeout; |
||
20 | |||
21 | protected function configure() |
||
22 | { |
||
23 | $this |
||
24 | ->setName('odm:schema:update') |
||
25 | ->addOption('class', 'c', InputOption::VALUE_OPTIONAL, 'Document class to process (default: all classes)') |
||
26 | ->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout (ms) for acknowledged index creation') |
||
27 | ->setDescription('Update indexes for your documents'); |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @return int |
||
32 | */ |
||
33 | protected function execute(InputInterface $input, OutputInterface $output) |
||
34 | { |
||
35 | $class = $input->getOption('class'); |
||
36 | $timeout = $input->getOption('timeout'); |
||
37 | $this->timeout = is_string($timeout) ? (int) $timeout : null; |
||
38 | |||
39 | $sm = $this->getSchemaManager(); |
||
40 | $isErrored = false; |
||
41 | |||
42 | try { |
||
43 | View Code Duplication | if (is_string($class)) { |
|
0 ignored issues
–
show
|
|||
44 | $this->processDocumentIndex($sm, $class); |
||
45 | $output->writeln(sprintf('Updated <comment>index(es)</comment> for <info>%s</info>', $class)); |
||
46 | } else { |
||
47 | $this->processIndex($sm); |
||
48 | $output->writeln('Updated <comment>indexes</comment> for <info>all classes</info>'); |
||
49 | } |
||
50 | } catch (Throwable $e) { |
||
51 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
52 | $isErrored = true; |
||
53 | } |
||
54 | |||
55 | return $isErrored ? 255 : 0; |
||
56 | } |
||
57 | |||
58 | protected function processDocumentIndex(SchemaManager $sm, string $document) |
||
59 | { |
||
60 | $sm->updateDocumentIndexes($document, $this->timeout); |
||
61 | } |
||
62 | |||
63 | protected function processIndex(SchemaManager $sm) |
||
64 | { |
||
65 | $sm->updateIndexes($this->timeout); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @throws BadMethodCallException |
||
70 | */ |
||
71 | protected function processDocumentCollection(SchemaManager $sm, string $document) |
||
72 | { |
||
73 | throw new BadMethodCallException('Cannot update a document collection'); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @throws BadMethodCallException |
||
78 | */ |
||
79 | protected function processCollection(SchemaManager $sm) |
||
80 | { |
||
81 | throw new BadMethodCallException('Cannot update a collection'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @throws BadMethodCallException |
||
86 | */ |
||
87 | protected function processDocumentDb(SchemaManager $sm, string $document) |
||
88 | { |
||
89 | throw new BadMethodCallException('Cannot update a document database'); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @throws BadMethodCallException |
||
94 | */ |
||
95 | protected function processDb(SchemaManager $sm) |
||
96 | { |
||
97 | throw new BadMethodCallException('Cannot update a database'); |
||
98 | } |
||
99 | } |
||
100 |
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.