Completed
Push — master ( 92e389...871de3 )
by Maciej
16s
created

CreateCommand::execute()   D

Complexity

Conditions 10
Paths 56

Size

Total Lines 38
Code Lines 24

Duplication

Lines 18
Ratio 47.37 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 18
loc 38
ccs 0
cts 31
cp 0
rs 4.8196
c 1
b 0
f 0
cc 10
eloc 24
nc 56
nop 2
crap 110

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::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::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections')
24
            ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes')
25
            ->setDescription('Create databases, collections and indexes for your documents')
26
        ;
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        $create = array_filter($this->createOrder, function ($option) use ($input) {
32
            return $input->getOption($option);
33
        });
34
35
        // Default to the full creation order if no options were specified
36
        $create = empty($create) ? $this->createOrder : $create;
37
38
        $class = $input->getOption('class');
39
40
        $timeout = $input->getOption('timeout');
41
        $this->timeout = isset($timeout) ? (int) $timeout : null;
42
43
        $sm = $this->getSchemaManager();
44
        $isErrored = false;
45
46 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...
47
            try {
48
                if (isset($class)) {
49
                    $this->{'processDocument' . ucfirst($option)}($sm, $class);
50
                } else {
51
                    $this->{'process' . ucfirst($option)}($sm);
52
                }
53
                $output->writeln(sprintf(
54
                    'Created <comment>%s%s</comment> for <info>%s</info>',
55
                    $option,
56
                    (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')),
57
                    ($class ?? 'all classes')
58
                ));
59
            } catch (\Exception $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, $document)
69
    {
70
        $sm->createDocumentCollection($document);
71
    }
72
73
    protected function processCollection(SchemaManager $sm)
74
    {
75
        $sm->createCollections();
76
    }
77
78
    protected function processDocumentDb(SchemaManager $sm, $document)
79
    {
80
        throw new \BadMethodCallException('A database is created automatically by MongoDB (>= 3.0).');
81
    }
82
83
    protected function processDb(SchemaManager $sm)
84
    {
85
        throw new \BadMethodCallException('A database is created automatically by MongoDB (>= 3.0).');
86
    }
87
88
    protected function processDocumentIndex(SchemaManager $sm, $document)
89
    {
90
        $sm->ensureDocumentIndexes($document, $this->timeout);
91
    }
92
93
    protected function processIndex(SchemaManager $sm)
94
    {
95
        $sm->ensureIndexes($this->timeout);
96
    }
97
98
    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...
99
    {
100
        $classMetadata = $this->getMetadataFactory()->getMetadataFor($document);
101
102
        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...
103
            $this->getDocumentManager()->getProxyFactory()->generateProxyClasses(array($classMetadata));
104
        }
105
    }
106
107
    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...
108
    {
109
        $classes = array_filter($this->getMetadataFactory()->getAllMetadata(), function (ClassMetadata $classMetadata) {
110
            return !$classMetadata->isEmbeddedDocument && !$classMetadata->isMappedSuperclass && !$classMetadata->isQueryResultDocument;
111
        });
112
113
        $this->getDocumentManager()->getProxyFactory()->generateProxyClasses($classes);
114
    }
115
}
116