Completed
Pull Request — master (#1219)
by Maciej
12:55
created

GeneratePersistentCollectionsCommand::execute()   C

Complexity

Conditions 10
Paths 28

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 54
ccs 0
cts 45
cp 0
rs 6.8372
cc 10
eloc 32
nc 28
nop 2
crap 110

How to fix   Long Method    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
 * 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;
21
22
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
23
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console;
27
28
/**
29
 * Command to (re)generate the persistent collection classes used by doctrine.
30
 *
31
 * @since 1.1
32
 */
33
class GeneratePersistentCollectionsCommand extends Console\Command\Command
34
{
35
    /**
36
     * @see Console\Command\Command
37
     */
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('odm:generate:persistent-collections')
42
            ->setDescription('Generates persistent collection classes for custom collections.')
43
            ->setDefinition(array(
44
                new InputOption(
45
                    'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
46
                    'A string pattern used to match documents that should be processed.'
47
                ),
48
                new InputArgument(
49
                    'dest-path', InputArgument::OPTIONAL,
50
                    'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.'
51
                ),
52
            ))
53
            ->setHelp(<<<EOT
54
Generates persistent collection classes for custom collections.
55
EOT
56
            );
57
    }
58
59
    /**
60
     * @see Console\Command\Command
61
     */
62
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
63
    {
64
        $dm = $this->getHelper('documentManager')->getDocumentManager();
65
66
        $metadatas = $dm->getMetadataFactory()->getAllMetadata();
67
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
68
69
        // Process destination directory
70
        if (($destPath = $input->getArgument('dest-path')) === null) {
71
            $destPath = $dm->getConfiguration()->getPersistentCollectionDir();
72
        }
73
74
        if ( ! is_dir($destPath)) {
75
            mkdir($destPath, 0775, true);
76
        }
77
78
        $destPath = realpath($destPath);
79
80
        if ( ! file_exists($destPath)) {
81
            throw new \InvalidArgumentException(
82
                sprintf("Persistent collections destination directory '<info>%s</info>' does not exist.", $destPath)
83
            );
84
        } elseif ( ! is_writable($destPath)) {
85
            throw new \InvalidArgumentException(
86
                sprintf("Persistent collections destination directory '<info>%s</info>' does not have write permissions.", $destPath)
87
            );
88
        }
89
90
        if (count($metadatas)) {
91
            $generated = [];
92
            $collectionGenerator = $dm->getConfiguration()->getPersistentCollectionGenerator();
93
            foreach ($metadatas as $metadata) {
94
                $output->write(
95
                    sprintf('Processing document "<info>%s</info>"', $metadata->name) . PHP_EOL
96
                );
97
                foreach ($metadata->getAssociationNames() as $fieldName) {
98
                    $mapping = $metadata->getFieldMapping($fieldName);
99
                    if (empty($mapping['collectionClass']) || isset($generated[$mapping['collectionClass']])) {
100
                        continue;
101
                    }
102
                    $generated[$mapping['collectionClass']] = true;
103
                    $output->write(
104
                        sprintf('Generating class for "<info>%s</info>"', $mapping['collectionClass']) . PHP_EOL
105
                    );
106
                    $collectionGenerator->generateClass($mapping['collectionClass'], $destPath);
107
                }
108
            }
109
110
            // Outputting information message
111
            $output->write(PHP_EOL . sprintf('Persistent collections classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
112
        } else {
113
            $output->write('No Metadata Classes to process.' . PHP_EOL);
114
        }
115
    }
116
}
117