Passed
Pull Request — master (#6843)
by Luís
19:24 queued 01:03
created

GenerateRepositoriesCommand::execute()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 50
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.3484

Importance

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