Completed
Push — 1.0.x ( bcb4f8...f4d4ca )
by Andreas
9s
created

GenerateRepositoriesCommand::execute()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 36
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 26
nc 9
nop 2
crap 56
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 Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console;
25
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter;
26
use Doctrine\ODM\MongoDB\Tools\DocumentRepositoryGenerator;
27
28
/**
29
 * Command to generate repository classes for mapping information.
30
 *
31
 * @since   1.0
32
 * @author  Benjamin Eberlei <[email protected]>
33
 * @author  Guilherme Blanco <[email protected]>
34
 * @author  Jonathan Wage <[email protected]>
35
 * @author  Roman Borschel <[email protected]>
36
 */
37
class GenerateRepositoriesCommand extends Console\Command\Command
38
{
39
    /**
40
     * @see Console\Command\Command
41
     */
42
    protected function configure()
43
    {
44
        $this
45
        ->setName('odm:generate:repositories')
46
        ->setDescription('Generate repository classes from your mapping information.')
47
        ->setDefinition(array(
48
            new InputOption(
49
                'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
50
                'A string pattern used to match documents that should be processed.'
51
            ),
52
            new InputArgument(
53
                'dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.'
54
            )
55
        ))
56
        ->setHelp(<<<EOT
57
Generate repository classes from your mapping information.
58
EOT
59
        );
60
    }
61
62
    /**
63
     * @see Console\Command\Command
64
     */
65
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
66
    {
67
        $dm = $this->getHelper('documentManager')->getDocumentManager();
68
        
69
        $metadatas = $dm->getMetadataFactory()->getAllMetadata();
70
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
71
72
        // Process destination directory
73
        $destPath = realpath($input->getArgument('dest-path'));
74
75
        if ( ! file_exists($destPath)) {
76
            throw new \InvalidArgumentException(
77
                sprintf("Documents destination directory '<info>%s</info>' does not exist.", $destPath)
78
            );
79
        } elseif ( ! is_writable($destPath)) {
80
            throw new \InvalidArgumentException(
81
                sprintf("Documents destination directory '<info>%s</info>' does not have write permissions.", $destPath)
82
            );
83
        }
84
85
        if (count($metadatas)) {
86
            $numRepositories = 0;
87
            $generator = new DocumentRepositoryGenerator();
88
89
            foreach ($metadatas as $metadata) {
90
                if ($metadata->customRepositoryClassName) {
91
                    $output->write(
92
                        sprintf('Processing repository "<info>%s</info>"', $metadata->customRepositoryClassName) . PHP_EOL
93
                    );
94
95
                    $generator->writeDocumentRepositoryClass($metadata->customRepositoryClassName, $destPath);
96
97
                    $numRepositories++;
98
                }
99
            }
100
101
            if ($numRepositories) {
102
                // Outputting information message
103
                $output->write(PHP_EOL . sprintf('Repository classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
104
            } else {
105
                $output->write('No Repository classes were found to be processed.' . PHP_EOL);
106
            }
107
        } else {
108
            $output->write('No Metadata Classes to process.' . PHP_EOL);
109
        }
110
    }
111
}
112