ExportValidateCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace TreeHouse\IoBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
11
use TreeHouse\IoBundle\Export\FeedExporter;
12
use TreeHouse\IoBundle\Export\FeedType\FeedTypeInterface;
13
14
class ExportValidateCommand extends Command
15
{
16
    /**
17
     * @var FeedExporter
18
     */
19
    protected $exporter;
20
21
    /**
22
     * @var \XMLReader
23
     */
24
    protected $reader;
25
26
    /**
27
     * @var string
28
     */
29
    protected $currentItem;
30
31
    /**
32
     * @param FeedExporter $exporter
33
     */
34
    public function __construct(FeedExporter $exporter)
35
    {
36
        $this->exporter = $exporter;
37
38
        parent::__construct();
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    protected function configure()
45
    {
46
        $this->addArgument('type', InputArgument::REQUIRED, 'The type for which the feed needs to be validated');
47
        $this->setName('io:export:validate');
48
        $this->setDescription('Validate an exported feed');
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        $type = $this->exporter->getType($input->getArgument('type'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('type') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, TreeHouse\IoBundle\Export\FeedExporter::getType() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
57
58
        try {
59
            $this->validate($type, $output);
60
        } catch (\RuntimeException $exception) {
61
            $output->writeln('');
62
            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
63
            $output->writeln('');
64
            $output->writeln('Node:');
65
            $output->writeln($this->currentItem);
66
67
            return 1;
68
        }
69
70
        $output->writeln('');
71
        $output->writeln(sprintf('<info>Feed "%s" is valid!</info>', $type->getName()));
72
73
        return 0;
74
    }
75
76
    /**
77
     * @param FeedTypeInterface $type
78
     * @param OutputInterface   $output
79
     *
80
     * @return int
81
     */
82
    protected function validate(FeedTypeInterface $type, OutputInterface $output)
83
    {
84
        $file = $this->exporter->getFeedFilename($type);
85
86
        if (!file_exists($file)) {
87
            throw new FileNotFoundException(sprintf('<error>Feed "%s" has not yet been exported</error>', $type->getName()));
88
        }
89
90
        $options = LIBXML_NOENT | LIBXML_COMPACT | LIBXML_PARSEHUGE | LIBXML_NOERROR | LIBXML_NOWARNING;
91
        $this->reader = new \XMLReader($options);
0 ignored issues
show
Unused Code introduced by
The call to XMLReader::__construct() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
92
        $this->reader->open($file);
93
        $this->reader->setParserProperty(\XMLReader::SUBST_ENTITIES, true);
94
//        foreach ($type->getNamespaces() as $name => $location) {
95
//            $this->reader->setSchema($location);
96
//        }
97
98
        libxml_clear_errors();
99
        libxml_use_internal_errors(true);
100
        libxml_disable_entity_loader(true);
101
102
        $progress = new ProgressBar($output);
103
        $progress->start();
104
105
        // go through the whole thing
106
        while ($this->reader->read()) {
107
            if ($this->reader->nodeType === \XMLReader::ELEMENT && $this->reader->name === $type->getItemNode()) {
108
                $progress->advance();
109
                $this->currentItem = $this->reader->readOuterXml();
110
            }
111
112
            if ($error = libxml_get_last_error()) {
113
                throw new \RuntimeException(
114
                    sprintf(
115
                        '[%s %s] %s (in %s - line %d, column %d)',
116
                        LIBXML_ERR_WARNING === $error->level ? 'WARNING' : 'ERROR',
117
                        $error->code,
118
                        trim($error->message),
119
                        $error->file ? $error->file : 'n/a',
120
                        $error->line,
121
                        $error->column
122
                    )
123
                );
124
            }
125
        }
126
127
        $progress->finish();
128
    }
129
}
130