Completed
Pull Request — master (#603)
by Jeroen De
01:20
created

GeoJsonContent::todoStoreSomeSmwStuff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 2
cts 2
cp 1
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Maps\MediaWiki\Content;
4
5
use FormatJson;
6
use Maps\MapsFactory;
7
use Maps\Presentation\GeoJsonMapPageUi;
8
use Maps\Presentation\OutputFacade;
9
use ParserOptions;
10
use ParserOutput;
11
use Status;
12
use Title;
13
14
class GeoJsonContent extends \JsonContent {
15
16
	public const CONTENT_MODEL_ID = 'GeoJSON';
17
18
	public static function newEmptyContentString(): string {
19
		$text = '{"type": "FeatureCollection", "features": []}';
20
		return FormatJson::encode( FormatJson::parse( $text )->getValue(), true, FormatJson::UTF8_OK );
21
	}
22
23 5
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
24 5
		parent::__construct(
25 5
			$text,
26
			$modelId
27
		);
28 5
	}
29
30 5
	public function getData(): Status {
31 5
		$status = parent::getData();
32
33 5
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
34 1
			return Status::newFatal( 'Invalid GeoJson' );
35
		}
36
37 4
		return $status;
38
	}
39
40 5
	private function isGeoJson( $json ): bool {
41 5
		return property_exists( $json, 'type' )
42 5
			&& $json->type === 'FeatureCollection'
43 5
			&& property_exists( $json, 'features' )
44 5
			&& is_array( $json->features );
45
	}
46
47 3
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
48
		$generateHtml, ParserOutput &$output ) {
49
50 3
		if ( !$generateHtml || !$this->isValid() ) {
51
			$output->setText( '' );
52
			return;
53
		}
54
55 3
		$this->addMapHtmlToOutput( $output );
56
57 3
		$this->storeSemanticValues( $title, $output );
58 3
	}
59
60 3
	private function addMapHtmlToOutput( ParserOutput $output ) {
61 3
		( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) );
62 3
	}
63
64 3
	private function storeSemanticValues( Title $title, ParserOutput $output ) {
65 3
		if ( MapsFactory::globalInstance()->smwIntegrationIsEnabled() ) {
66 3
			MapsFactory::globalInstance()->newSemanticGeoJsonStore( $output, $title )->storeGeoJson( $this->mText );
67
		}
68 3
	}
69
70
}
71