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

GzJsonImportCommand::executeCommand()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 17
nc 2
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
use Wikibase\JsonDumpReader\SeekableDumpReader;
12
13
/**
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class GzJsonImportCommand extends ImportCommandBase {
18
19
	protected function configure() {
20
		$this->setName( 'import:gz' );
21
		$this->setDescription( 'Imports entities from a gzip compressed JSON dump' );
22
23
		$this->addArgument(
24
			'file',
25
			InputArgument::REQUIRED,
26
			'Full path of the gz JSON dump file'
27
		);
28
29
		$this->addOption(
30
			'continue',
31
			'c',
32
			InputOption::VALUE_OPTIONAL,
33
			'The position to resume import from'
34
		);
35
36
		$this->addOption(
37
			'max',
38
			'm',
39
			InputOption::VALUE_OPTIONAL,
40
			'The maximum number of entities to import'
41
		);
42
	}
43
44
	protected function executeCommand( InputInterface $input, OutputInterface $output ) {
45
		$dumpReader = ( new JsonDumpFactory() )->newGzDumpReader(
46
			$input->getArgument( 'file' ),
47
			is_numeric( $input->getOption( 'continue' ) ) ? (int)$input->getOption( 'continue' ) : 0
48
		);
49
50
		$importer = new PagesImporterCli(
51
			$input,
52
			$output,
53
			$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...
54
			function() use ( $output, $dumpReader ) {
55
				$output->writeln( "\n" );
56
				$output->writeln( "<info>Import process aborted</info>" );
57
				$output->writeln( "<comment>To resume, run with</comment> --continue=" . $dumpReader->getPosition() );
58
			}
59
		);
60
61
		$iterator = $this->factory->newJsonEntityPageIterator( $dumpReader );
62
63
		if ( is_numeric( $input->getOption( 'max' ) ) ) {
64
			$iterator = new \LimitIterator( $iterator, 0, (int)$input->getOption( 'max' ) );
65
		}
66
67
		$importer->runImport( $iterator );
68
69
		$this->outputMaxContinuation( $input, $output, $dumpReader );
70
	}
71
72
	private function outputMaxContinuation( InputInterface $input, OutputInterface $output, SeekableDumpReader $reader ) {
73
		if ( is_numeric( $input->getOption( 'max' ) ) ) {
74
			$output->writeln(
75
				"\n<comment>To continue from current position, run with</comment> --continue=" . $reader->getPosition()
76
			);
77
		}
78
	}
79
80
}
81