GenerateProxiesCommand::execute()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 56

Duplication

Lines 15
Ratio 26.79 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 15
loc 56
ccs 0
cts 40
cp 0
rs 7.4044
c 0
b 0
f 0
cc 9
nc 9
nop 2
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Tools\Console\Command;
6
7
use Doctrine\ODM\MongoDB\ConfigurationException;
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
10
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter;
11
use InvalidArgumentException;
12
use Symfony\Component\Console;
13
use Symfony\Component\Console\Input\InputOption;
14
use const PHP_EOL;
15
use function array_filter;
16
use function assert;
17
use function count;
18
use function file_exists;
19
use function is_array;
20
use function is_dir;
21
use function is_string;
22
use function is_writable;
23
use function mkdir;
24
use function realpath;
25
use function sprintf;
26
27
/**
28
 * Command to (re)generate the proxy classes used by doctrine.
29
 */
30
class GenerateProxiesCommand extends Console\Command\Command
31
{
32
    /**
33
     * @see Console\Command\Command
34
     */
35
    protected function configure()
36
    {
37
        $this
38
        ->setName('odm:generate:proxies')
39
        ->setDescription('Generates proxy classes for document classes.')
40
        ->setDefinition([
41
            new InputOption(
42
                'filter',
43
                null,
44
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
45
                'A string pattern used to match documents that should be processed.'
46
            ),
47
        ])
48
        ->setHelp(<<<EOT
49
Generates proxy classes for document classes.
50
EOT
51
        );
52
    }
53
54
    /**
55
     * @see Console\Command\Command
56
     */
57
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
58
    {
59
        $filter = $input->getOption('filter');
60
        assert(is_array($filter));
61
62
        /** @var DocumentManager $dm */
63
        $dm = $this->getHelper('documentManager')->getDocumentManager();
64
65
        /** @var ClassMetadata[] $metadatas */
66
        $metadatas = array_filter($dm->getMetadataFactory()->getAllMetadata(), static function (ClassMetadata $classMetadata) : bool {
67
            return ! $classMetadata->isEmbeddedDocument && ! $classMetadata->isMappedSuperclass && ! $classMetadata->isQueryResultDocument;
68
        });
69
        $metadatas = MetadataFilter::filter($metadatas, $filter);
70
        $destPath  = $dm->getConfiguration()->getProxyDir();
71
72
        if (! is_string($destPath)) {
73
            throw ConfigurationException::proxyDirMissing();
74
        }
75
76
        if (! is_dir($destPath)) {
77
            mkdir($destPath, 0775, true);
78
        }
79
80
        $destPath = realpath($destPath);
81
        assert($destPath !== false);
82
83
        if (! file_exists($destPath)) {
84
            throw new InvalidArgumentException(
85
                sprintf("Proxies destination directory '<info>%s</info>' does not exist.", $destPath)
86
            );
87
        }
88
89
        if (! is_writable($destPath)) {
90
            throw new InvalidArgumentException(
91
                sprintf("Proxies destination directory '<info>%s</info>' does not have write permissions.", $destPath)
92
            );
93
        }
94
95 View Code Duplication
        if (count($metadatas)) {
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...
96
            foreach ($metadatas as $metadata) {
97
                $output->write(
98
                    sprintf('Processing document "<info>%s</info>"', $metadata->name) . PHP_EOL
99
                );
100
            }
101
102
            // Generating Proxies
103
            $dm->getProxyFactory()->generateProxyClasses($metadatas);
104
105
            // Outputting information message
106
            $output->write(PHP_EOL . sprintf('Proxy classes generated to "<info>%s</info>"', $destPath) . PHP_EOL);
107
        } else {
108
            $output->write('No Metadata Classes to process.' . PHP_EOL);
109
        }
110
111
        return 0;
112
    }
113
}
114