Completed
Push — master ( 36e04f...8ec39e )
by Jeroen De
04:55 queued 03:58
created

TimelinePresenter::createJsonString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ModernTimeline;
6
7
use ModernTimeline\ResultFacade\SimpleQueryResult;
8
use ModernTimeline\ResultFacade\ResultPresenter;
9
use ModernTimeline\SlidePresenter\SimpleSlidePresenter;
10
use ModernTimeline\SlidePresenter\SlidePresenter;
11
use ModernTimeline\SlidePresenter\TemplateSlidePresenter;
12
use SMWOutputs;
13
14
class TimelinePresenter implements ResultPresenter {
15
16
	private $id;
17
18 3
	public function __construct() {
19 3
		$this->id = $this->newTimelineId();
20 3
	}
21
22 3
	private function newTimelineId(): string {
23 3
		static $timelineNumber = 0;
24 3
		return 'modern_timeline_' . ++$timelineNumber;
25
	}
26
27 3
	public function presentResult( SimpleQueryResult $result ): string {
28 3
		SMWOutputs::requireResource( 'ext.modern.timeline' );
29
30 3
		SMWOutputs::requireHeadItem(
31 3
			$this->id,
32 3
			$this->createJs( $this->createJsonString( $result ) )
33
		);
34
35 3
		return $this->createDiv( $result->getParameters() );
36
	}
37
38 3
	private function createJsonString( SimpleQueryResult $result ) {
39 3
		$preJson = $this->newJsonBuilder( $result )
40 3
			->eventsToTimelineJson( ( new EventExtractor( $result->getParameters() ) )->extractEvents( $result->getSubjects() ) );
41
42 3
		$preJson['options'] = TimelineOptions::processedParamsToJson( $result->getParameters() );
43
44 3
		return json_encode( $preJson );
45
	}
46
47 3
	private function newJsonBuilder( SimpleQueryResult $result ): JsonBuilder {
48 3
		return new JsonBuilder(
49 3
			$this->getSlidePresenter( $result )
50
		);
51
	}
52
53 3
	private function getSlidePresenter( SimpleQueryResult $result ): SlidePresenter {
54 3
		$templateName = $result->getParameters()['template'];
55
56 3
		if ( $templateName === '' ) {
57 3
			return new SimpleSlidePresenter( $result->getProcessingResult()->getParameterArray() );
58
		}
59
60
		return new TemplateSlidePresenter( $templateName );
61
	}
62
63 3
	private function createJs( string $json ): string {
64 3
		return \Html::rawElement(
65 3
			'script',
66
			[
67 3
				'type' => 'text/javascript'
68
			],
69
			"if (!window.hasOwnProperty('modernTimeline')) {window.modernTimeline = {};}"
70 3
			. "\n window.modernTimeline.{$this->id} = $json;"
71
		);
72
	}
73
74 3
	private function createDiv( array $parameters ): string {
75 3
		$width = $parameters[TimelineOptions::PARAM_WIDTH];
76 3
		$height = $parameters[TimelineOptions::PARAM_HEIGHT];
77
78 3
		return \Html::rawElement(
79 3
			'div',
80
			[
81 3
				'id' => $this->id,
82 3
				'style' => "width: $width; height: $height",
83 3
				'class' => 'modern_timeline_outer_div'
84
			],
85 3
			\Html::element(
86 3
				'div',
87
				[
88 3
					'class' => 'modern_timeline_inner_div',
89
					'style' => 'width: 100%; height: calc(100% - 10px); background-color: rgba(0, 0, 0, 0.05); margin-top: 5px; margin-bottom: 5px;'
90
				]
91
			)
92
		);
93
	}
94
95
}
96