TimelinePresenter::createJsonString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
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;
0 ignored issues
show
Bug introduced by
The type SMWOutputs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use MediaWiki\Html\Html;
0 ignored issues
show
Bug introduced by
The type MediaWiki\Html\Html was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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