XmlDumpImportCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 2
cbo 8
dl 0
loc 57
ccs 33
cts 36
cp 0.9167
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 17 1
A executeCommand() 0 12 1
A getDumpIterator() 0 7 1
A handleContinueArgument() 0 9 2
A newDumpReader() 0 4 1
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