GeoJsonContent::getData()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\GeoJsonPages;
6
7
use FormatJson;
8
use Maps\MapsFactory;
9
use Maps\Presentation\OutputFacade;
10
use ParserOptions;
11
use ParserOutput;
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 6
	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) {
25 6
		parent::__construct(
26 6
			$text,
27
			$modelId
28
		);
29 6
	}
30
31 6
	public function getData(): Status {
32 6
		$status = parent::getData();
33
34 6
		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) {
35 1
			return Status::newFatal( 'Invalid GeoJson' );
36
		}
37
38 5
		return $status;
39
	}
40
41 6
	private function isGeoJson( $json ): bool {
42 6
		return property_exists( $json, 'type' )
43 6
			&& $json->type === 'FeatureCollection'
44 6
			&& property_exists( $json, 'features' )
45 6
			&& is_array( $json->features );
46
	}
47
48 4
	protected function fillParserOutput( Title $title, $revId, ParserOptions $options,
49
		$generateHtml, ParserOutput &$output ) {
50
51 4
		if ( !$generateHtml || !$this->isValid() ) {
52
			$output->setText( '' );
53
			return;
54
		}
55
56 4
		$this->addMapHtmlToOutput( $output );
57
58 4
		$this->storeSemanticValues( $title, $output );
59 4
	}
60
61 4
	private function addMapHtmlToOutput( ParserOutput $output ) {
62 4
		( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) );
63 4
	}
64
65 4
	private function storeSemanticValues( Title $title, ParserOutput $output ) {
66 4
		if ( MapsFactory::globalInstance()->smwIntegrationIsEnabled() ) {
67 4
			MapsFactory::globalInstance()->newSemanticGeoJsonStore( $output, $title )->storeGeoJson( $this->mText );
68
		}
69 4
	}
70
71
}
72