Completed
Push — master ( e5e84c...1b930b )
by Jeroen De
03:48
created

GeoJsonContent   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 82.76%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 2
dl 0
loc 70
ccs 24
cts 29
cp 0.8276
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A newEmptyContentString() 0 4 1
A __construct() 0 6 1
A getData() 0 9 3
A isGeoJson() 0 6 4
A fillParserOutput() 0 12 3
A addMapHtmlToOutput() 0 3 1
A todoStoreSomeSmwStuff() 0 17 1
1
<?php
2
3
namespace Maps\MediaWiki\Content;
4
5
use FormatJson;
6
use Maps\Presentation\GeoJsonMapPageUi;
7
use Maps\Presentation\OutputFacade;
8
use ParserOptions;
9
use ParserOutput;
10
use SMW\ApplicationFactory;
11
use SMW\DIProperty;
12
use Status;
13
use Title;
14
15
class GeoJsonContent extends \JsonContent {
16
17
	public const CONTENT_MODEL_ID = 'GeoJSON';
18
19
	public static function newEmptyContentString(): string {
20
		$text = '{"type": "FeatureCollection", "features": []}';
21
		return FormatJson::encode( FormatJson::parse( $text )->getValue(), true, FormatJson::UTF8_OK );
22
	}
23
24 5
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
25 5
		parent::__construct(
26 5
			$text,
27
			$modelId
28
		);
29 5
	}
30
31 5
	public function getData(): Status {
32 5
		$status = parent::getData();
33
34 5
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
35 1
			return Status::newFatal( 'Invalid GeoJson' );
36
		}
37
38 4
		return $status;
39
	}
40
41 5
	private function isGeoJson( $json ): bool {
42 5
		return property_exists( $json, 'type' )
43 5
			&& $json->type === 'FeatureCollection'
44 5
			&& property_exists( $json, 'features' )
45 5
			&& is_array( $json->features );
46
	}
47
48 3
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
49
		$generateHtml, ParserOutput &$output ) {
50
51 3
		if ( !$generateHtml || !$this->isValid() ) {
52
			$output->setText( '' );
53
			return;
54
		}
55
56 3
		$this->addMapHtmlToOutput( $output );
57
58 3
		$this->todoStoreSomeSmwStuff( $title, $output );
59 3
	}
60
61 3
	private function addMapHtmlToOutput( ParserOutput $output ) {
62 3
		( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) );
63 3
	}
64
65
	// TODO
66 3
	private function todoStoreSomeSmwStuff( Title $title, ParserOutput $output ) {
67 3
		return;
68
69
		$parserData = ApplicationFactory::getInstance()->newParserData( $title, $output );
0 ignored issues
show
Unused Code introduced by
$parserData = \SMW\Appli...rData($title, $output); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70
71
		$parserData->getSemanticData()->addPropertyObjectValue(
72
			new DIProperty( 'HasNumber' ),
73
			new \SMWDINumber( 42 )
74
		);
75
76
		$parserData->copyToParserOutput();
77
78
		ApplicationFactory::getInstance()->getEventDispatcher()->dispatch(
79
			'InvalidateEntityCache',
80
			[ 'title' => $title, 'context' => 'GeoJsonContent' ]
81
		);
82
	}
83
84
}
85