ApiImportCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 90.7%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 10
dl 0
loc 81
ccs 39
cts 43
cp 0.907
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 33 1
A setHttp() 0 3 1
A executeCommand() 0 5 1
A getEntityPageIterator() 0 28 4
1
<?php
2
3
namespace Queryr\Replicator\Cli\Command;
4
5
use BatchingIterator\BatchingIterator;
6
use Queryr\Replicator\Cli\Import\PagesImporterCli;
7
use Queryr\Replicator\EntityIdListNormalizer;
8
use Queryr\Replicator\EntitySource\Api\GetEntitiesClient;
9
use Queryr\Replicator\EntitySource\Api\Http;
10
use Queryr\Replicator\EntitySource\BatchingEntityPageFetcher;
11
use Queryr\Replicator\EntitySource\ReferencedEntityPageIterator;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Wikibase\DataModel\Entity\BasicEntityIdParser;
17
18
/**
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class ApiImportCommand extends ImportCommandBase {
23
24 1
	protected function configure() {
25 1
		$this->setName( 'import:api' );
26 1
		$this->setDescription( 'Imports entities via a Wikibase Repo web API' );
27
28 1
		$this->addArgument(
29 1
			'entities',
30 1
			InputArgument::IS_ARRAY,
31 1
			'The IDs of the entities to import, separated by spaces. ID ranges can be specified: Q1-Q100'
32
		);
33
34
//		$this->addOption(
35
//			'url',
36
//			null,
37
//			InputOption::VALUE_OPTIONAL,
38
//			'The full url of the Wikibase Repo web API to use',
39
//			'https://www.wikidata.org/w/api.php'
40
//		);
41
42 1
		$this->addOption(
43 1
			'batchsize',
44 1
			'b',
45 1
			InputOption::VALUE_OPTIONAL,
46 1
			'The number of API requests to bundle together',
47 1
			10
48
		);
49
50 1
		$this->addOption(
51 1
			'include-references',
52 1
			'r',
53 1
			InputOption::VALUE_NONE,
54 1
			'If referenced entities should also be imported'
55
		);
56 1
	}
57
58
	/**
59
	 * @var Http|null
60
	 */
61
	private $http;
62
63 1
	public function setHttp( Http $http ) {
64 1
		$this->http = $http;
65 1
	}
66
67 1
	protected function executeCommand( InputInterface $input, OutputInterface $output ) {
68 1
		$importer = new PagesImporterCli( $input, $output, $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...
69
70 1
		$importer->runImport( $this->getEntityPageIterator( $input ) );
71 1
	}
72
73 1
	private function getEntityPageIterator( InputInterface $input ): \Iterator {
74 1
		$http = $this->http === null ? new Http() : $this->http;
75
76 1
		$idListNormalizer = new EntityIdListNormalizer( new BasicEntityIdParser() );
77 1
		$ids = [];
78
79 1
		foreach ( $idListNormalizer->getNormalized( $input->getArgument( 'entities' ) ) as $id ) {
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('entities') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type null or string; however, Queryr\Replicator\Entity...alizer::getNormalized() does only seem to accept array<integer,string>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80 1
			  $ids[] = $id->getSerialization();
81
		}
82
83 1
		$batchingFetcher = new BatchingEntityPageFetcher(
84 1
			new GetEntitiesClient( $http ),
85 1
			$ids
86
		);
87
88 1
		$iterator = new BatchingIterator( $batchingFetcher );
89 1
		$iterator->setMaxBatchSize( (int)$input->getOption( 'batchsize' ) );
90
91 1
		if ( $input->getOption( 'include-references' ) ) {
92
			return new ReferencedEntityPageIterator(
93
				$iterator,
94
				$batchingFetcher,
95
				$this->factory->newLegacyEntityDeserializer()
96
			);
97
		}
98
99 1
		return $iterator;
100
	}
101
102
}
103