Completed
Pull Request — master (#1219)
by Maciej
10:06
created

GenerateProxiesCommand::execute()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 52
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 52
ccs 0
cts 43
cp 0
rs 7.2397
cc 7
eloc 23
nc 20
nop 2
crap 56

How to fix   Long Method   

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
 * @author  Maciej Malarz <[email protected]>
33
 */
34
class GenerateProxiesCommand extends Console\Command\Command
35
{
36
    /**
37
     * @see Console\Command\Command
38
     */
39
    protected function configure()
40
    {
41
        $this
42
            ->setName('odm:generate:persistent-collections')
43
            ->setDescription('Generates persistent collection classes for custom collections.')
44
            ->setDefinition(array(
45
                new InputOption(
46
                    'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
47
                    'A string pattern used to match documents that should be processed.'
48
                ),
49
                new InputArgument(
50
                    'dest-path', InputArgument::OPTIONAL,
51
                    'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.'
52
                ),
53
            ))
54
            ->setHelp(<<<EOT
55
Generates persistent collection classes for custom collections.
56
EOT
57
            );
58
    }
59
60
    /**
61
     * @see Console\Command\Command
62
     */
63
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
64
    {
65
        $dm = $this->getHelper('documentManager')->getDocumentManager();
66
67
        $metadatas = $dm->getMetadataFactory()->getAllMetadata();
68
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
69
70
        // Process destination directory
71
        if (($destPath = $input->getArgument('dest-path')) === null) {
72
            $destPath = $dm->getConfiguration()->getPersistentCollectionDir();
73
        }
74
75
        if ( ! is_dir($destPath)) {
76
            mkdir($destPath, 0775, true);
77
        }
78
79
        $destPath = realpath($destPath);
80
81
        if ( ! file_exists($destPath)) {
82
            throw new \InvalidArgumentException(
83
                sprintf("Persistent collections destination directory '<info>%s</info>' does not exist.", $destPath)
84
            );
85
        } elseif ( ! is_writable($destPath)) {
86
            throw new \InvalidArgumentException(
87
                sprintf("Persistent collections destination directory '<info>%s</info>' does not have write permissions.", $destPath)
88
            );
89
        }
90
91
        if (count($metadatas)) {
92
            $collFactory = $dm->getUnitOfWork()->getPersistentCollectionFactory();
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'])) {
100
                        continue;
101
                    }
102
                    $output->write(
103
                        sprintf('Generating class for "<info>%s</info>"', $mapping['collectionClass']) . PHP_EOL
104
                    );
105
                    $collFactory->generateClass($mapping['collectionClass'], $destPath);
106
                }
107
            }
108
109
            // Outputting information message
110
            $output->write(PHP_EOL . sprintf('Persistent collections classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
111
        } else {
112
            $output->write('No Metadata Classes to process.' . PHP_EOL);
113
        }
114
    }
115
}
116