Failed Conditions
Pull Request — 2.6 (#7506)
by
unknown
09:52
created

ConvertDoctrine1SchemaCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Test Coverage

Coverage 52.86%

Importance

Changes 0
Metric Value
wmc 18
eloc 64
dl 0
loc 168
ccs 37
cts 70
cp 0.5286
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setEntityGenerator() 0 3 1
A setMetadataExporter() 0 3 1
A execute() 0 18 1
B convertDoctrine1Schema() 0 62 10
A getEntityGenerator() 0 7 2
A getMetadataExporter() 0 7 2
A configure() 0 12 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
use Symfony\Component\Console\Style\SymfonyStyle;
31
32
/**
33
 * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file.
34
 *
35
 * @link    www.doctrine-project.org
36
 * @since   2.0
37
 * @author  Benjamin Eberlei <[email protected]>
38
 * @author  Guilherme Blanco <[email protected]>
39
 * @author  Jonathan Wage <[email protected]>
40
 * @author  Roman Borschel <[email protected]>
41
 *
42
 * @deprecated 2.7 This class is being removed from the ORM and won't have any replacement
43
 */
44
class ConvertDoctrine1SchemaCommand extends Command
45
{
46
    /**
47
     * @var EntityGenerator|null
48
     */
49
    private $entityGenerator = null;
50
51
    /**
52
     * @var ClassMetadataExporter|null
53
     */
54
    private $metadataExporter = null;
55
56
    /**
57
     * @return EntityGenerator
58
     */
59 1
    public function getEntityGenerator()
60
    {
61 1
        if ($this->entityGenerator == null) {
62
            $this->entityGenerator = new EntityGenerator();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\ORM\Tools\EntityGenerator has been deprecated: 2.7 This class is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

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

62
            $this->entityGenerator = /** @scrutinizer ignore-deprecated */ new EntityGenerator();
Loading history...
63
        }
64
65 1
        return $this->entityGenerator;
66
    }
67
68
    /**
69
     * @param EntityGenerator $entityGenerator
70
     *
71
     * @return void
72
     */
73 1
    public function setEntityGenerator(EntityGenerator $entityGenerator)
74
    {
75 1
        $this->entityGenerator = $entityGenerator;
76 1
    }
77
78
    /**
79
     * @return ClassMetadataExporter
80
     */
81 1
    public function getMetadataExporter()
82
    {
83 1
        if ($this->metadataExporter == null) {
84 1
            $this->metadataExporter = new ClassMetadataExporter();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\ORM\Tools\Export\ClassMetadataExporter has been deprecated: 2.7 This class is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

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

84
            $this->metadataExporter = /** @scrutinizer ignore-deprecated */ new ClassMetadataExporter();
Loading history...
85
        }
86
87 1
        return $this->metadataExporter;
88
    }
89
90
    /**
91
     * @param ClassMetadataExporter $metadataExporter
92
     *
93
     * @return void
94
     */
95
    public function setMetadataExporter(ClassMetadataExporter $metadataExporter)
96
    {
97
        $this->metadataExporter = $metadataExporter;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    protected function configure()
104
    {
105 1
        $this->setName('orm:convert-d1-schema')
106 1
             ->setAliases(['orm:convert:d1-schema'])
107 1
             ->setDescription('Converts Doctrine 1.x schema into a Doctrine 2.x schema')
108 1
             ->addArgument('from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.')
109 1
             ->addArgument('to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.')
110 1
             ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your Doctrine 2.X mapping information.')
111 1
             ->addOption('from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Optional paths of Doctrine 1.X schema information.', [])
112 1
             ->addOption('extend', null, InputOption::VALUE_OPTIONAL, 'Defines a base class to be extended by generated entity classes.')
113 1
             ->addOption('num-spaces', null, InputOption::VALUE_OPTIONAL, 'Defines the number of indentation spaces', 4)
114 1
             ->setHelp('Converts Doctrine 1.x schema into a Doctrine 2.x schema.');
115 1
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    protected function execute(InputInterface $input, OutputInterface $output)
121
    {
122
        $ui = new SymfonyStyle($input, $output);
123
        $ui->warning('Command ' . $this->getName() . ' is deprecated and will be removed in Doctrine 3.0.');
124
125
        // Process source directories
126
        $fromPaths = array_merge([$input->getArgument('from-path')], $input->getOption('from'));
0 ignored issues
show
Bug introduced by
It seems like $input->getOption('from') can also be of type boolean and string; however, parameter $array2 of array_merge() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

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

126
        $fromPaths = array_merge([$input->getArgument('from-path')], /** @scrutinizer ignore-type */ $input->getOption('from'));
Loading history...
127
128
        // Process destination directory
129
        $destPath = realpath($input->getArgument('dest-path'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('dest-path') can also be of type string[]; however, parameter $path of realpath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

129
        $destPath = realpath(/** @scrutinizer ignore-type */ $input->getArgument('dest-path'));
Loading history...
130
131
        $toType = $input->getArgument('to-type');
132
        $extend = $input->getOption('extend');
133
        $numSpaces = $input->getOption('num-spaces');
134
135
        $this->convertDoctrine1Schema($fromPaths, $destPath, $toType, $numSpaces, $extend, $output);
0 ignored issues
show
Bug introduced by
It seems like $toType can also be of type string[]; however, parameter $toType of Doctrine\ORM\Tools\Conso...onvertDoctrine1Schema() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

135
        $this->convertDoctrine1Schema($fromPaths, $destPath, /** @scrutinizer ignore-type */ $toType, $numSpaces, $extend, $output);
Loading history...
136
137
        return 0;
138
    }
139
140
    /**
141
     * @param array           $fromPaths
142
     * @param string          $destPath
143
     * @param string          $toType
144
     * @param int             $numSpaces
145
     * @param string|null     $extend
146
     * @param OutputInterface $output
147
     *
148
     * @throws \InvalidArgumentException
149
     */
150 1
    public function convertDoctrine1Schema(array $fromPaths, $destPath, $toType, $numSpaces, $extend, OutputInterface $output)
151
    {
152 1
        foreach ($fromPaths as &$dirName) {
153
            $dirName = realpath($dirName);
154
155
            if ( ! file_exists($dirName)) {
156
                throw new \InvalidArgumentException(
157
                    sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)
158
                );
159
            }
160
161
            if ( ! is_readable($dirName)) {
162
                throw new \InvalidArgumentException(
163
                    sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName)
164
                );
165
            }
166
        }
167
168 1
        if ( ! file_exists($destPath)) {
169
            throw new \InvalidArgumentException(
170
                sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)
171
            );
172
        }
173
174 1
        if ( ! is_writable($destPath)) {
175
            throw new \InvalidArgumentException(
176
                sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)
177
            );
178
        }
179
180 1
        $cme = $this->getMetadataExporter();
181 1
        $exporter = $cme->getExporter($toType, $destPath);
182
183 1
        if (strtolower($toType) === 'annotation') {
184 1
            $entityGenerator = $this->getEntityGenerator();
185 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

185
            $exporter->/** @scrutinizer ignore-call */ 
186
                       setEntityGenerator($entityGenerator);
Loading history...
186
187 1
            $entityGenerator->setNumSpaces($numSpaces);
188
189 1
            if ($extend !== null) {
190
                $entityGenerator->setClassToExtend($extend);
191
            }
192
        }
193
194 1
        $converter = new ConvertDoctrine1Schema($fromPaths);
195 1
        $metadata = $converter->getMetadata();
196
197 1
        if ($metadata) {
198
            $output->writeln('');
199
200
            foreach ($metadata as $class) {
201
                $output->writeln(sprintf('Processing entity "<info>%s</info>"', $class->name));
202
            }
203
204
            $exporter->setMetadata($metadata);
205
            $exporter->export();
206
207
            $output->writeln(PHP_EOL . sprintf(
208
                'Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath
209
            ));
210
        } else {
211 1
            $output->writeln('No Metadata Classes to process.');
212
        }
213 1
    }
214
}
215