Completed
Push — master ( ce8c34...f078de )
by Andreas
49:52 queued 47:00
created

CreateCommand::execute()   C

Complexity

Conditions 11
Paths 112

Size

Total Lines 42
Code Lines 26

Duplication

Lines 18
Ratio 42.86 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 18
loc 42
ccs 0
cts 34
cp 0
rs 5.0769
c 0
b 0
f 0
cc 11
eloc 26
nc 112
nop 2
crap 132

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;
4
5
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
6
use Doctrine\ODM\MongoDB\SchemaManager;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class CreateCommand extends AbstractCommand
12
{
13
    private $createOrder = array(self::DB, self::COLLECTION, self::INDEX);
14
15
    private $timeout;
16
17
    protected function configure()
18
    {
19
        $this
20
            ->setName('odm:schema:create')
21
            ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)')
22
            ->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout (ms) for acknowledged index creation')
23
            ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Create databases')
24
            ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections')
25
            ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes')
26
            ->setDescription('Create databases, collections and indexes for your documents')
27
        ;
28
    }
29
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        if ($input->getOption(self::DB)) {
33
            @trigger_error('The ' . self::DB . ' option is deprecated and will be removed in ODM 2.0', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
34
        }
35
36
        $create = array_filter($this->createOrder, function ($option) use ($input) {
37
            return $input->getOption($option);
38
        });
39
40
        // Default to the full creation order if no options were specified
41
        $create = empty($create) ? $this->createOrder : $create;
42
43
        $class = $input->getOption('class');
44
45
        $timeout = $input->getOption('timeout');
46
        $this->timeout = isset($timeout) ? (int) $timeout : null;
47
48
        $sm = $this->getSchemaManager();
49
        $isErrored = false;
50
51 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...
52
            try {
53
                if (isset($class)) {
54
                    $this->{'processDocument' . ucfirst($option)}($sm, $class);
55
                } else {
56
                    $this->{'process' . ucfirst($option)}($sm);
57
                }
58
                $output->writeln(sprintf(
59
                    'Created <comment>%s%s</comment> for <info>%s</info>',
60
                    $option,
61
                    (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')),
62
                    ($class ?? 'all classes')
63
                ));
64
            } catch (\Exception $e) {
65
                $output->writeln('<error>' . $e->getMessage() . '</error>');
66
                $isErrored = true;
67
            }
68
        }
69
70
        return $isErrored ? 255 : 0;
71
    }
72
73
    protected function processDocumentCollection(SchemaManager $sm, $document)
74
    {
75
        $sm->createDocumentCollection($document);
76
    }
77
78
    protected function processCollection(SchemaManager $sm)
79
    {
80
        $sm->createCollections();
81
    }
82
83
    protected function processDocumentDb(SchemaManager $sm, $document)
84
    {
85
        $sm->createDocumentDatabase($document);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ODM\MongoDB\Sch...reateDocumentDatabase() has been deprecated with message: A database is created automatically by MongoDB (>= 3.0). Deprecated since ODM 1.2, to be removed in ODM 2.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86
    }
87
88
    protected function processDb(SchemaManager $sm)
89
    {
90
        $sm->createDatabases();
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ODM\MongoDB\Sch...ager::createDatabases() has been deprecated with message: Databases are created automatically by MongoDB (>= 3.0). Deprecated since ODM 1.2, to be removed in ODM 2.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
91
    }
92
93
    protected function processDocumentIndex(SchemaManager $sm, $document)
94
    {
95
        $sm->ensureDocumentIndexes($document, $this->timeout);
96
    }
97
98
    protected function processIndex(SchemaManager $sm)
99
    {
100
        $sm->ensureIndexes($this->timeout);
101
    }
102
103
    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...
104
    {
105
        $classMetadata = $this->getMetadataFactory()->getMetadataFor($document);
106
107
        if (!$classMetadata->isEmbeddedDocument && !$classMetadata->isMappedSuperclass && !$classMetadata->isQueryResultDocument) {
0 ignored issues
show
Bug introduced by
Accessing isEmbeddedDocument on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing isMappedSuperclass on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing isQueryResultDocument on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
108
            $this->getDocumentManager()->getProxyFactory()->generateProxyClasses(array($classMetadata));
109
        }
110
    }
111
112
    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...
113
    {
114
        $classes = array_filter($this->getMetadataFactory()->getAllMetadata(), function (ClassMetadata $classMetadata) {
115
            return !$classMetadata->isEmbeddedDocument && !$classMetadata->isMappedSuperclass && !$classMetadata->isQueryResultDocument;
116
        });
117
118
        $this->getDocumentManager()->getProxyFactory()->generateProxyClasses($classes);
119
    }
120
}
121