ResourceFormatter::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace SRF;
4
5
use Html;
6
use SMWOutputs as ResourceManager;
7
use SMWQueryResult as QueryResult;
8
9
/**
10
 * @since 3.0
11
 *
12
 * @license GNU GPL v2 or later
13
 * @author mwjames
14
 */
15
class ResourceFormatter {
16
17
	/**
18
	 * @since 3.0
19
	 *
20
	 * @param array $modules
21
	 * @param array $styleModules
22
	 */
23 3
	public static function registerResources( array $modules = [], array $styleModules = [] ) {
24
25 3
		foreach ( $modules as $module ) {
26 2
			ResourceManager::requireResource( $module );
27
		}
28
29 3
		foreach ( $styleModules as $styleModule ) {
30 3
			ResourceManager::requireStyle( $styleModule );
31
		}
32 3
	}
33
34
	/**
35
	 * @since 3.0
36
	 *
37
	 * @return string
38
	 */
39 3
	public static function session() {
40 3
		return 'smw-' . uniqid();
41
	}
42
43
	/**
44
	 * Convenience method generating a visual placeholder before any
45
	 * JS is registered to indicate that resources (JavaScript, CSS)
46
	 * are being loaded and once ready ensure to set
47
	 * ( '.smw-spinner' ).hide()
48
	 *
49
	 * @since 3.0
50
	 */
51 3
	public static function placeholder() {
52 3
		self::registerResources( [], [ 'ext.smw.style' ] );
53
54 3
		return Html::rawElement(
55 3
			'div',
56 3
			[ 'class' => 'srf-loading-dots' ]
57
		);
58
	}
59
60
	/**
61
	 *
62
	 * @since 3.0
63
	 *
64
	 * @param string $id
65
	 * @param array $data
66
	 */
67 2
	public static function encode( $id, $data ) {
68 2
		ResourceManager::requireHeadItem(
69 2
			$id,
70 2
			\Skin::makeVariablesScript(
71
				[
72 2
					$id => json_encode( $data )
73
				],
74 2
				false
75
			)
76
		);
77 2
	}
78
79
	/**
80
	 * @param QueryResult $queryResult
81
	 * @param $outputMode
82
	 *
83
	 * @return string
84
	 */
85 2
	public static function getData( QueryResult $queryResult, $outputMode, $parameters = [] ) {
0 ignored issues
show
Unused Code introduced by mwjames
The parameter $outputMode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
87
		// Add parameters that are only known to the specific printer
88 2
		$ask = $queryResult->getQuery()->toArray();
89
90 2
		foreach ( $parameters as $key => $value ) {
91 2
			if ( is_string( $value ) || is_integer( $value ) || is_bool( $value ) ) {
92 2
				$ask['parameters'][$key] = $value;
93
			}
94
		}
95
96
		// Combine all data into one object
97
		$data = [
98
			'query' => [
99 2
				'result' => $queryResult->toArray(),
100 2
				'ask' => $ask
101
			]
102
		];
103
104 2
		return $data;
105
	}
106
107
}
108