Completed
Pull Request — 1.0.x (#1416)
by Maciej
09:32
created

CreateCommand::processDocumentDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;
21
22
use Doctrine\ODM\MongoDB\SchemaManager;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
27
/**
28
 * @author Bulat Shakirzyanov <[email protected]>
29
 */
30
class CreateCommand extends AbstractCommand
31
{
32
    private $createOrder = array(self::DB, self::COLLECTION, self::INDEX);
33
34
    private $timeout;
35
36
    protected function configure()
37
    {
38
        $this
39
            ->setName('odm:schema:create')
40
            ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)')
41
            ->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout (ms) for acknowledged index creation')
42
            ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Create databases')
43
            ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections')
44
            ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes')
45
            ->setDescription('Create databases, collections and indexes for your documents')
46
        ;
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        foreach ($this->createOrder as $option) {
52
            if ($input->getOption($option)) {
53
                $create[] = $option;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$create was never initialized. Although not strictly required by PHP, it is generally a good practice to add $create = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
54
            }
55
        }
56
57
        // Default to the full creation order if no options were specified
58
        $create = empty($create) ? $this->createOrder : $create;
59
60
        $class = $input->getOption('class');
61
62
        $timeout = $input->getOption('timeout');
63
        $this->timeout = isset($timeout) ? (int) $timeout : null;
64
65
        $sm = $this->getSchemaManager();
66
        $isErrored = false;
67
68 View Code Duplication
        foreach ($create as $option) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
69
            try {
70
                if (isset($class)) {
71
                    $this->{'processDocument' . ucfirst($option)}($sm, $class);
72
                } else {
73
                    $this->{'process' . ucfirst($option)}($sm);
74
                }
75
                $output->writeln(sprintf(
76
                    'Created <comment>%s%s</comment> for <info>%s</info>',
77
                    $option,
78
                    (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')),
79
                    (isset($class) ? $class : 'all classes')
80
                ));
81
            } catch (\Exception $e) {
82
                $output->writeln('<error>' . $e->getMessage() . '</error>');
83
                $isErrored = true;
84
            }
85
        }
86
87
        return ($isErrored) ? 255 : 0;
88
    }
89
90
    protected function processDocumentCollection(SchemaManager $sm, $document)
91
    {
92
        $sm->createDocumentCollection($document);
93
    }
94
95
    protected function processCollection(SchemaManager $sm)
96
    {
97
        $sm->createCollections();
98
    }
99
100
    protected function processDocumentDb(SchemaManager $sm, $document)
101
    {
102
        $sm->createDocumentDatabase($document);
103
    }
104
105
    protected function processDb(SchemaManager $sm)
106
    {
107
        $sm->createDatabases();
108
    }
109
110
    protected function processDocumentIndex(SchemaManager $sm, $document)
111
    {
112
        $sm->ensureDocumentIndexes($document, $this->timeout);
113
    }
114
115
    protected function processIndex(SchemaManager $sm)
116
    {
117
        $sm->ensureIndexes($this->timeout);
118
    }
119
120
    protected function processDocumentProxy(SchemaManager $sm, $document)
0 ignored issues
show
Unused Code introduced by
The parameter $sm is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        $this->getDocumentManager()->getProxyFactory()->generateProxyClasses(array($this->getMetadataFactory()->getMetadataFor($document)));
123
    }
124
125
    protected function processProxy(SchemaManager $sm)
0 ignored issues
show
Unused Code introduced by
The parameter $sm is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
    {
127
        $this->getDocumentManager()->getProxyFactory()->generateProxyClasses($this->getMetadataFactory()->getAllMetadata());
128
    }
129
}
130