Completed
Push — master ( d01339...b9998b )
by Jeroen De
02:51
created

Bz2JsonImportCommand::execute()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
rs 8.5806
cc 4
eloc 20
nc 5
nop 2
1
<?php
2
3
namespace Queryr\Replicator\Cli\Command;
4
5
use Queryr\Replicator\Cli\Import\PagesImporterCli;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Wikibase\JsonDumpReader\JsonDumpFactory;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class Bz2JsonImportCommand extends ImportCommandBase {
17
18
	protected function configure() {
19
		$this->setName( 'import:bz2' );
20
		$this->setDescription( 'Imports entities from a bzip2 compressed JSON dump' );
21
22
		$this->addArgument(
23
			'file',
24
			InputArgument::REQUIRED,
25
			'Full path of the bz2 JSON dump file'
26
		);
27
28
		$this->addOption(
29
			'max',
30
			'm',
31
			InputOption::VALUE_OPTIONAL,
32
			'The maximum number of entities to import'
33
		);
34
	}
35
36
	protected function executeCommand( InputInterface $input, OutputInterface $output ) {
37
		$importer = new PagesImporterCli(
38
			$input,
39
			$output,
40
			$this->factory,
0 ignored issues
show
Bug introduced by
It seems like $this->factory can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
41
			function() use ( $output ) {
42
				$output->writeln( "\n" );
43
				$output->writeln( "<info>Import process aborted</info>" );
44
			}
45
		);
46
47
		$dumpReader = ( new JsonDumpFactory() )->newBz2DumpReader( $input->getArgument( 'file' ) );
48
		$iterator = $this->factory->newJsonEntityPageIterator( $dumpReader );
49
50
		if ( is_numeric( $input->getOption( 'max' ) ) ) {
51
			$iterator = new \LimitIterator( $iterator, 0, (int)$input->getOption( 'max' ) );
52
		}
53
54
		$importer->runImport( $iterator );
55
	}
56
57
}
58