XmlDumpImportCommand::executeCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 9
cp 0.6667
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1.037
1
<?php
2
3
namespace Queryr\Replicator\Cli\Command;
4
5
use Queryr\DumpReader\DumpReader;
6
use Queryr\DumpReader\ReaderFactory;
7
use Queryr\Replicator\Cli\Import\PagesImporterCli;
8
use Queryr\Replicator\EntitySource\Dump\DumpEntityPageIterator;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class XmlDumpImportCommand extends ImportCommandBase {
19
20 2
	protected function configure() {
21 2
		$this->setName( 'import:xml' );
22 2
		$this->setDescription( 'Imports entities from an extracted XML dump' );
23
24 2
		$this->addArgument(
25 2
			'file',
26 2
			InputArgument::REQUIRED,
27 2
			'Full path of the XML dump'
28
		);
29
30 2
		$this->addOption(
31 2
			'continue',
32 2
			'c',
33 2
			InputOption::VALUE_OPTIONAL,
34 2
			'The title to resume from (title not included)'
35
		);
36 2
	}
37
38
	protected function executeCommand( InputInterface $input, OutputInterface $output ) {
39 2
		$onAborted = function( $pageTitle ) use ( $output ) {
40
			$output->writeln( "\n" );
41
			$output->writeln( "<info>Import process aborted</info>" );
42
			$output->writeln( "<comment>To resume, run with</comment> --continue=$pageTitle" );
43 2
		};
44
45 2
		$importer = new PagesImporterCli( $input, $output, $this->factory, $onAborted );
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...
46
47 2
		$entityPageIterator = new DumpEntityPageIterator( $this->getDumpIterator( $input, $output ) );
48 2
		$importer->runImport( $entityPageIterator );
49 2
	}
50
51 2
	private function getDumpIterator( InputInterface $input, OutputInterface $output ) {
52 2
		$reader = $this->newDumpReader( $input->getArgument( 'file' ) );
53
54 2
		$this->handleContinueArgument( $input, $output, $reader );
55
56 2
		return $reader->getIterator();
57
	}
58
59 2
	private function handleContinueArgument( InputInterface $input, OutputInterface $output, DumpReader $reader ) {
60 2
		$continueTitle = $input->getOption( 'continue' );
61
62 2
		if ( $continueTitle !== null ) {
63 1
			$output->write( "<info>Seeking to title </info><comment>$continueTitle</comment><info>... </info>" );
64 1
			$reader->seekToTitle( $continueTitle );
65 1
			$output->writeln( "<info>done</info>" );
66
		}
67 2
	}
68
69 2
	private function newDumpReader( $file ) {
70 2
		$dumpReaderFactory = new ReaderFactory();
71 2
		return $dumpReaderFactory->newDumpReaderForFile( $file );
72
	}
73
74
}
75