Failed Conditions
Pull Request — master (#6655)
by Luís
15:29
created

ConvertDoctrine1SchemaCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Test Coverage

Coverage 55.07%

Importance

Changes 0
Metric Value
dl 0
loc 167
ccs 38
cts 69
cp 0.5507
rs 10
c 0
b 0
f 0
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setEntityGenerator() 0 3 1
A getEntityGenerator() 0 7 2
A getMetadataExporter() 0 7 2
A setMetadataExporter() 0 3 1
A execute() 0 13 1
C convertDoctrine1Schema() 0 62 10
A configure() 0 13 1
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\ORM\Tools\Console\Command;
21
22
use Doctrine\ORM\Tools\ConvertDoctrine1Schema;
23
use Doctrine\ORM\Tools\EntityGenerator;
24
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
/**
32
 * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file.
33
 *
34
 * @link    www.doctrine-project.org
35
 * @since   2.0
36
 * @author  Benjamin Eberlei <[email protected]>
37
 * @author  Guilherme Blanco <[email protected]>
38
 * @author  Jonathan Wage <[email protected]>
39
 * @author  Roman Borschel <[email protected]>
40
 */
41
class ConvertDoctrine1SchemaCommand extends Command
42
{
43
    /**
44
     * @var EntityGenerator|null
45
     */
46
    private $entityGenerator = null;
47
48
    /**
49
     * @var ClassMetadataExporter|null
50
     */
51
    private $metadataExporter = null;
52
53
    /**
54
     * @return EntityGenerator
55
     */
56 1
    public function getEntityGenerator()
57
    {
58 1
        if ($this->entityGenerator == null) {
59
            $this->entityGenerator = new EntityGenerator();
60
        }
61
62 1
        return $this->entityGenerator;
63
    }
64
65
    /**
66
     * @param EntityGenerator $entityGenerator
67
     *
68
     * @return void
69
     */
70 1
    public function setEntityGenerator(EntityGenerator $entityGenerator)
71
    {
72 1
        $this->entityGenerator = $entityGenerator;
73 1
    }
74
75
    /**
76
     * @return ClassMetadataExporter
77
     */
78 1
    public function getMetadataExporter()
79
    {
80 1
        if ($this->metadataExporter == null) {
81 1
            $this->metadataExporter = new ClassMetadataExporter();
82
        }
83
84 1
        return $this->metadataExporter;
85
    }
86
87
    /**
88
     * @param ClassMetadataExporter $metadataExporter
89
     *
90
     * @return void
91
     */
92
    public function setMetadataExporter(ClassMetadataExporter $metadataExporter)
93
    {
94
        $this->metadataExporter = $metadataExporter;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1
    protected function configure()
101
    {
102
        $this
103 1
            ->setName('orm:convert-d1-schema')
104 1
            ->setAliases(['orm:convert:d1-schema'])
105 1
            ->setDescription('Converts Doctrine 1.x schema into a Doctrine 2.x schema')
106 1
            ->addArgument('from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.')
107 1
            ->addArgument('to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.')
108 1
            ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your Doctrine 2.X mapping information.')
109 1
            ->addOption('from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Optional paths of Doctrine 1.X schema information.', [])
110 1
            ->addOption('extend', null, InputOption::VALUE_OPTIONAL, 'Defines a base class to be extended by generated entity classes.')
111 1
            ->addOption('num-spaces', null, InputOption::VALUE_OPTIONAL, 'Defines the number of indentation spaces', 4)
112 1
            ->setHelp(<<<EOT
113 1
Converts Doctrine 1.x schema into a Doctrine 2.x schema.
114
EOT
115
            );
116 1
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    protected function execute(InputInterface $input, OutputInterface $output)
122
    {
123
        // Process source directories
124
        $fromPaths = array_merge([$input->getArgument('from-path')], $input->getOption('from'));
125
126
        // Process destination directory
127
        $destPath = realpath($input->getArgument('dest-path'));
128
129
        $toType = $input->getArgument('to-type');
130
        $extend = $input->getOption('extend');
131
        $numSpaces = $input->getOption('num-spaces');
132
133
        $this->convertDoctrine1Schema($fromPaths, $destPath, $toType, $numSpaces, $extend, $output);
134
    }
135
136
    /**
137
     * @param array           $fromPaths
138
     * @param string          $destPath
139
     * @param string          $toType
140
     * @param int             $numSpaces
141
     * @param string|null     $extend
142
     * @param OutputInterface $output
143
     *
144
     * @throws \InvalidArgumentException
145
     */
146 1
    public function convertDoctrine1Schema(array $fromPaths, $destPath, $toType, $numSpaces, $extend, OutputInterface $output)
147
    {
148 1
        foreach ($fromPaths as &$dirName) {
149
            $dirName = realpath($dirName);
150
151
            if ( ! file_exists($dirName)) {
152
                throw new \InvalidArgumentException(
153
                    sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)
154
                );
155
            }
156
157
            if ( ! is_readable($dirName)) {
158
                throw new \InvalidArgumentException(
159
                    sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName)
160
                );
161
            }
162
        }
163
164 1
        if ( ! file_exists($destPath)) {
165
            throw new \InvalidArgumentException(
166
                sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)
167
            );
168
        }
169
170 1
        if ( ! is_writable($destPath)) {
171
            throw new \InvalidArgumentException(
172
                sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)
173
            );
174
        }
175
176 1
        $cme = $this->getMetadataExporter();
177 1
        $exporter = $cme->getExporter($toType, $destPath);
178
179 1
        if (strtolower($toType) === 'annotation') {
180 1
            $entityGenerator = $this->getEntityGenerator();
181 1
            $exporter->setEntityGenerator($entityGenerator);
0 ignored issues
show
Bug introduced by
The method setEntityGenerator() does not exist on Doctrine\ORM\Tools\Export\Driver\AbstractExporter. It seems like you code against a sub-type of Doctrine\ORM\Tools\Export\Driver\AbstractExporter such as Doctrine\ORM\Tools\Expor...iver\AnnotationExporter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
            $exporter->/** @scrutinizer ignore-call */ 
182
                       setEntityGenerator($entityGenerator);
Loading history...
182
183 1
            $entityGenerator->setNumSpaces($numSpaces);
184
185 1
            if ($extend !== null) {
186
                $entityGenerator->setClassToExtend($extend);
187
            }
188
        }
189
190 1
        $converter = new ConvertDoctrine1Schema($fromPaths);
191 1
        $metadata = $converter->getMetadata();
192
193 1
        if ($metadata) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $metadata of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
194
            $output->writeln('');
195
196
            foreach ($metadata as $class) {
197
                $output->writeln(sprintf('Processing entity "<info>%s</info>"', $class->name));
198
            }
199
200
            $exporter->setMetadata($metadata);
201
            $exporter->export();
202
203
            $output->writeln(PHP_EOL . sprintf(
204
                'Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath
205
            ));
206
        } else {
207 1
            $output->writeln('No Metadata Classes to process.');
208
        }
209 1
    }
210
}
211