Completed
Pull Request — master (#1787)
by Stefano
21:31
created

GeneratePersistentCollectionsCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 32.95 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

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

2 Methods

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