Completed
Push — master ( 4009e3...35344a )
by Andreas
10s
created

GeneratePersistentCollectionsCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 34.52 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 29
loc 84
ccs 0
cts 65
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 20 20 1
C execute() 9 54 10

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