Completed
Pull Request — master (#13)
by Jeroen De
03:37
created

TimelinePresenter::getTemplateName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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