Code Duplication    Length = 74-74 lines in 2 locations

lib/Doctrine/ODM/MongoDB/Tools/Console/Command/GenerateHydratorsCommand.php 1 location

@@ 36-109 (lines=74) @@
33
 * @author  Jonathan Wage <[email protected]>
34
 * @author  Roman Borschel <[email protected]>
35
 */
36
class GenerateHydratorsCommand extends Console\Command\Command
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

lib/Doctrine/ODM/MongoDB/Tools/Console/Command/GenerateProxiesCommand.php 1 location

@@ 36-109 (lines=74) @@
33
 * @author  Jonathan Wage <[email protected]>
34
 * @author  Roman Borschel <[email protected]>
35
 */
36
class GenerateProxiesCommand extends Console\Command\Command
37
{
38
    /**
39
     * @see Console\Command\Command
40
     */
41
    protected function configure()
42
    {
43
        $this
44
        ->setName('odm:generate:proxies')
45
        ->setDescription('Generates proxy 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 proxy classes. If none is provided, it will attempt to grab from configuration.'
54
            ),
55
        ))
56
        ->setHelp(<<<EOT
57
Generates proxy 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()->getProxyDir();
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("Proxies destination directory '<info>%s</info>' does not exist.", $destPath)
86
            );
87
        } elseif ( ! is_writable($destPath)) {
88
            throw new \InvalidArgumentException(
89
                sprintf("Proxies 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 Proxies
101
            $dm->getProxyFactory()->generateProxyClasses($metadatas, $destPath);
102
103
            // Outputting information message
104
            $output->write(PHP_EOL . sprintf('Proxy 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