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\Mapping\ClassMetadata; |
||
9 | use Doctrine\ODM\MongoDB\SchemaManager; |
||
10 | use Symfony\Component\Console\Input\InputInterface; |
||
11 | use Symfony\Component\Console\Input\InputOption; |
||
12 | use Symfony\Component\Console\Output\OutputInterface; |
||
13 | use Throwable; |
||
14 | use function array_filter; |
||
15 | use function assert; |
||
16 | use function is_string; |
||
17 | use function sprintf; |
||
18 | use function ucfirst; |
||
19 | |||
20 | class CreateCommand extends AbstractCommand |
||
21 | { |
||
22 | /** @var string[] */ |
||
23 | private $createOrder = [self::COLLECTION, self::INDEX]; |
||
24 | |||
25 | /** @var int|null */ |
||
26 | private $timeout; |
||
27 | |||
28 | protected function configure() |
||
29 | { |
||
30 | $this |
||
31 | ->setName('odm:schema:create') |
||
32 | ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
||
33 | ->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout (ms) for acknowledged index creation') |
||
34 | ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections') |
||
35 | ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes') |
||
36 | ->setDescription('Create databases, collections and indexes for your documents'); |
||
37 | } |
||
38 | |||
39 | protected function execute(InputInterface $input, OutputInterface $output) |
||
40 | { |
||
41 | $create = array_filter($this->createOrder, static function ($option) use ($input) { |
||
42 | return $input->getOption($option); |
||
43 | }); |
||
44 | |||
45 | // Default to the full creation order if no options were specified |
||
46 | $create = empty($create) ? $this->createOrder : $create; |
||
47 | |||
48 | $class = $input->getOption('class'); |
||
49 | $timeout = $input->getOption('timeout'); |
||
50 | $this->timeout = is_string($timeout) ? (int) $timeout : null; |
||
51 | |||
52 | $sm = $this->getSchemaManager(); |
||
53 | $isErrored = false; |
||
54 | |||
55 | View Code Duplication | foreach ($create as $option) { |
|
0 ignored issues
–
show
|
|||
56 | try { |
||
57 | if (isset($class)) { |
||
58 | $this->{'processDocument' . ucfirst($option)}($sm, $class); |
||
59 | } else { |
||
60 | $this->{'process' . ucfirst($option)}($sm); |
||
61 | } |
||
62 | $output->writeln(sprintf( |
||
63 | 'Created <comment>%s%s</comment> for <info>%s</info>', |
||
64 | $option, |
||
65 | is_string($class) ? ($option === self::INDEX ? '(es)' : '') : ($option === self::INDEX ? 'es' : 's'), |
||
66 | is_string($class) ? $class : 'all classes' |
||
67 | )); |
||
68 | } catch (Throwable $e) { |
||
69 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
70 | $isErrored = true; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | return $isErrored ? 255 : 0; |
||
75 | } |
||
76 | |||
77 | protected function processDocumentCollection(SchemaManager $sm, string $document) |
||
78 | { |
||
79 | $sm->createDocumentCollection($document); |
||
80 | } |
||
81 | |||
82 | protected function processCollection(SchemaManager $sm) |
||
83 | { |
||
84 | $sm->createCollections(); |
||
85 | } |
||
86 | |||
87 | protected function processDocumentDb(SchemaManager $sm, string $document) |
||
88 | { |
||
89 | throw new BadMethodCallException('A database is created automatically by MongoDB (>= 3.0).'); |
||
90 | } |
||
91 | |||
92 | protected function processDb(SchemaManager $sm) |
||
93 | { |
||
94 | throw new BadMethodCallException('A database is created automatically by MongoDB (>= 3.0).'); |
||
95 | } |
||
96 | |||
97 | protected function processDocumentIndex(SchemaManager $sm, string $document) |
||
98 | { |
||
99 | $sm->ensureDocumentIndexes($document, $this->timeout); |
||
100 | } |
||
101 | |||
102 | protected function processIndex(SchemaManager $sm) |
||
103 | { |
||
104 | $sm->ensureIndexes($this->timeout); |
||
105 | } |
||
106 | |||
107 | protected function processDocumentProxy(SchemaManager $sm, string $document) |
||
108 | { |
||
109 | $classMetadata = $this->getMetadataFactory()->getMetadataFor($document); |
||
110 | assert($classMetadata instanceof ClassMetadata); |
||
111 | |||
112 | $this->getDocumentManager()->getProxyFactory()->generateProxyClasses([$classMetadata]); |
||
113 | } |
||
114 | |||
115 | protected function processProxy(SchemaManager $sm) |
||
116 | { |
||
117 | /** @var ClassMetadata[] $metadatas */ |
||
118 | $metadatas = $this->getMetadataFactory()->getAllMetadata(); |
||
119 | $this->getDocumentManager()->getProxyFactory()->generateProxyClasses($metadatas); |
||
120 | } |
||
121 | } |
||
122 |
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.