Completed
Push — styling ( 65b228 )
by Jeroen De
04:06
created

GeoJsonFetcher   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 73
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A parse() 0 3 1
B fetch() 0 34 6
A newEmptyResult() 0 6 1
A normalizeJson() 0 13 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\DataAccess;
6
7
use FileFetcher\FileFetcher;
8
use FileFetcher\FileFetchingException;
9
use MediaWiki\Storage\RevisionLookup;
10
11
/**
12
 * Returns the content of the JSON file at the specified location as array.
13
 * Empty array is returned on failure.
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class GeoJsonFetcher {
19
20
	private $fileFetcher;
21
	private $titleParser;
22
	private $revisionLookup;
23
24 10
	public function __construct( FileFetcher $fileFetcher, \TitleParser $titleParser, RevisionLookup $revisionLookup ) {
25 10
		$this->fileFetcher = $fileFetcher;
26 10
		$this->titleParser = $titleParser;
27 10
		$this->revisionLookup = $revisionLookup;
28 10
	}
29
30 7
	public function parse( string $fileLocation ): array {
31 7
		return $this->fetch( $fileLocation )->getContent();
32
	}
33
34 10
	public function fetch( string $fileLocation ) {
35
		try {
36 10
			$title = $this->titleParser->parseTitle( $fileLocation, NS_GEO_JSON );
37 9
			$revision = $this->revisionLookup->getRevisionByTitle( $title );
38
39 9
			if ( $revision !== null ) {
40 4
				$content = $revision->getContent( 'main' );
41
42 4
				if ( $content instanceof \JsonContent ) {
0 ignored issues
show
Bug introduced by
The class JsonContent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
43 4
					return new GeoJsonFetcherResult(
44 9
						$this->normalizeJson( $content->getNativeData() ),
45
						$title
46
					);
47
				}
48
			}
49
		}
50 1
		catch ( \MalformedTitleException $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class MalformedTitleException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
51
		}
52
53
		// Prevent reading JSON files on the server
54 6
		if( !filter_var( $fileLocation, FILTER_VALIDATE_URL) ) {
55 3
			return $this->newEmptyResult();
56
		}
57
58
		try {
59 3
			return new GeoJsonFetcherResult(
60 3
				$this->normalizeJson( $this->fileFetcher->fetchFile( $fileLocation ) ),
61 2
				null
62
			);
63
		}
64 1
		catch ( FileFetchingException $ex ) {
65 1
			return $this->newEmptyResult();
66
		}
67
	}
68
69 4
	private function newEmptyResult(): GeoJsonFetcherResult {
70 4
		return new GeoJsonFetcherResult(
71 4
			[],
72 4
			null
73
		);
74
	}
75
76 6
	private function normalizeJson( ?string $jsonString ): array {
77 6
		if ( $jsonString === null ) {
78
			return [];
79
		}
80
81 6
		$json = json_decode( $jsonString, true );
82
83 6
		if ( $json === null ) {
84 1
			return [];
85
		}
86
87 5
		return $json;
88
	}
89
90
}
91