Completed
Push — master ( 0f5a81...3041fb )
by Jeroen De
9s
created

Bz2JsonImportCommand::executeCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0094

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 13
cts 15
cp 0.8667
rs 9.4285
cc 2
eloc 13
nc 2
nop 2
crap 2.0094
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 2
	protected function configure() {
19 2
		$this->setName( 'import:bz2' );
20 2
		$this->setDescription( 'Imports entities from a bzip2 compressed JSON dump' );
21
22 2
		$this->addArgument(
23 2
			'file',
24 2
			InputArgument::REQUIRED,
25 2
			'Full path of the bz2 JSON dump file'
26
		);
27
28 2
		$this->addOption(
29 2
			'max',
30 2
			'm',
31 2
			InputOption::VALUE_OPTIONAL,
32 2
			'The maximum number of entities to import'
33
		);
34 2
	}
35
36 2
	protected function executeCommand( InputInterface $input, OutputInterface $output ) {
37 2
		$importer = new PagesImporterCli(
38 2
			$input,
39 2
			$output,
40 2
			$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 2
			function() use ( $output ) {
42
				$output->writeln( "\n" );
43
				$output->writeln( "<info>Import process aborted</info>" );
44 2
			}
45
		);
46
47 2
		$dumpReader = ( new JsonDumpFactory() )->newBz2DumpReader( $input->getArgument( 'file' ) );
48 2
		$iterator = $this->factory->newJsonEntityPageIterator( $dumpReader );
49
50 2
		if ( is_numeric( $input->getOption( 'max' ) ) ) {
51 1
			$iterator = new \LimitIterator( $iterator, 0, (int)$input->getOption( 'max' ) );
52
		}
53
54 2
		$importer->runImport( $iterator );
55 2
	}
56
57
}
58