Completed
Push — master ( b76934...8d2b64 )
by Asmir
9s
created

ConvertToYaml::convert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 23
rs 9.0856
cc 2
eloc 14
nc 2
nop 4
1
<?php
2
namespace Goetas\Xsd\XsdToPhp\Command;
3
4
use Goetas\Xsd\XsdToPhp\Jms\PathGenerator\Psr4PathGenerator;
5
use Goetas\Xsd\XsdToPhp\Jms\YamlConverter;
6
use Symfony\Component\Yaml\Dumper;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Goetas\Xsd\XsdToPhp\AbstractConverter;
9
use Goetas\Xsd\XsdToPhp\Naming\NamingStrategy;
10
use Symfony\Component\Console\Helper\ProgressBar;
11
12
class ConvertToYaml extends AbstractConvert
13
{
14
15
    /**
16
     *
17
     * @see Console\Command\Command
18
     */
19
    protected function configure()
20
    {
21
        parent::configure();
22
        $this->setName('convert:jms-yaml');
23
        $this->setDescription('Convert XSD definitions into YAML metadata for JMS Serializer');
24
    }
25
26
    protected function getConverterter(NamingStrategy $naming)
27
    {
28
        return new YamlConverter($naming);
29
    }
30
31
    protected function convert(AbstractConverter $converter, array $schemas, array $targets, OutputInterface $output)
32
    {
33
        $items = $converter->convert($schemas);
34
35
        $dumper = new Dumper();
36
37
        $pathGenerator = new Psr4PathGenerator($targets);
38
39
        $progress = new ProgressBar($output, count($items));
40
        $progress->start();
41
42
        foreach ($items as $item) {
43
            $progress->advance();
44
            $output->write(" Item <info>" . key($item) . "</info>... ");
45
46
            $source = $dumper->dump($item, 10000);
47
            $output->write("created source... ");
48
49
            $path = $pathGenerator->getPath($item);
50
            $bytes = file_put_contents($path, $source);
51
            $output->writeln("saved source <comment>$bytes bytes</comment>.");
52
        }
53
    }
54
}
55