Completed
Pull Request — 1.0.x (#1416)
by Maciej
09:32
created

GenerateHydratorsCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 0
cbo 6
dl 74
loc 74
ccs 0
cts 53
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 20 20 1
C execute() 44 44 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
27
/**
28
 * Command to (re)generate the hydrator classes used by doctrine.
29
 *
30
 * @since   1.0
31
 * @author  Benjamin Eberlei <[email protected]>
32
 * @author  Guilherme Blanco <[email protected]>
33
 * @author  Jonathan Wage <[email protected]>
34
 * @author  Roman Borschel <[email protected]>
35
 */
36 View Code Duplication
class GenerateHydratorsCommand extends Console\Command\Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
37
{
38
    /**
39
     * @see Console\Command\Command
40
     */
41
    protected function configure()
42
    {
43
        $this
44
        ->setName('odm:generate:hydrators')
45
        ->setDescription('Generates hydrator classes for document classes.')
46
        ->setDefinition(array(
47
            new InputOption(
48
                'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
49
                'A string pattern used to match documents that should be processed.'
50
            ),
51
            new InputArgument(
52
                'dest-path', InputArgument::OPTIONAL,
53
                'The path to generate your hydrator classes. If none is provided, it will attempt to grab from configuration.'
54
            ),
55
        ))
56
        ->setHelp(<<<EOT
57
Generates hydrator classes for document classes.
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
        if (($destPath = $input->getArgument('dest-path')) === null) {
74
            $destPath = $dm->getConfiguration()->getHydratorDir();
75
        }
76
77
        if ( ! is_dir($destPath)) {
78
            mkdir($destPath, 0775, true);
79
        }
80
81
        $destPath = realpath($destPath);
82
83
        if ( ! file_exists($destPath)) {
84
            throw new \InvalidArgumentException(
85
                sprintf("Hydrators destination directory '<info>%s</info>' does not exist.", $destPath)
86
            );
87
        } elseif ( ! is_writable($destPath)) {
88
            throw new \InvalidArgumentException(
89
                sprintf("Hydrators destination directory '<info>%s</info>' does not have write permissions.", $destPath)
90
            );
91
        }
92
93
        if (count($metadatas)) {
94
            foreach ($metadatas as $metadata) {
95
                $output->write(
96
                    sprintf('Processing document "<info>%s</info>"', $metadata->name) . PHP_EOL
97
                );
98
            }
99
100
            // Generating Hydrators
101
            $dm->getHydratorFactory()->generateHydratorClasses($metadatas, $destPath);
102
103
            // Outputting information message
104
            $output->write(PHP_EOL . sprintf('Hydrator classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
105
        } else {
106
            $output->write('No Metadata Classes to process.' . PHP_EOL);
107
        }
108
    }
109
}
110