Passed
Push — master ( d8e403...d1c323 )
by Paul
05:05
created

Settings::buildFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use GeminiLabs\SiteReviews\Database\DefaultsManager;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html\Field;
9
use GeminiLabs\SiteReviews\Modules\Html\Template;
10
use GeminiLabs\SiteReviews\Modules\Translator;
11
12
class Settings
13
{
14
	/**
15
	 * @param string $id
16
	 * @return string
17
	 */
18
	public function buildFields( $id )
19
	{
20
		$method = glsr( Helper::class )->buildMethodName( $id, 'getTemplateContextFor' );
21
		$context = !method_exists( $this, $method )
22
			? $this->getTemplateContext( $id )
23
			: $this->$method( $id );
24
		return glsr( Template::class )->build( 'pages/settings/'.$id, [
25
			'context' => $context,
26
		]);
27
	}
28
29
	/**
30
	 * @param string $partialPath
31
	 * @return void
32
	 */
33
	public function render( $partialPath, array $args = [] )
34
	{
35
		echo $this->build( $partialPath, $args );
0 ignored issues
show
Bug introduced by
The method build() does not exist on GeminiLabs\SiteReviews\Modules\Html\Settings. Did you maybe mean buildFields()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
		echo $this->/** @scrutinizer ignore-call */ build( $partialPath, $args );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
	}
37
38
	/**
39
	 * @return array
40
	 */
41
	protected function getSettingFields( $path )
42
	{
43
		$settings = glsr( DefaultsManager::class )->settings();
44
		return array_filter( $settings, function( $key ) use( $path ) {
45
			return glsr( Helper::class )->startsWith( $path, $key );
46
		}, ARRAY_FILTER_USE_KEY );
47
	}
48
49
	/**
50
	 * @param string $id
51
	 * @return array
52
	 */
53
	protected function getTemplateContext( $id )
54
	{
55
		$fields = $this->getSettingFields( $this->normalizeSettingPath( $id ));
56
		$rows = '';
57
		foreach( $fields as $name => $field ) {
58
			$field = wp_parse_args( $field, [
59
				'is_setting' => true,
60
				'name' => $name,
61
			]);
62
			$rows.= new Field( $field );
63
		}
64
		return [
65
			'rows' => $rows,
66
		];
67
	}
68
69
	/**
70
	 * @return array
71
	 */
72
	protected function getTemplateContextForTranslations()
73
	{
74
		$translations = glsr( Translator::class )->renderAll();
75
		$class = empty( $translations )
76
			? 'glsr-hidden'
77
			: '';
78
		return [
79
			'class' => $class,
80
			'database_key' => OptionManager::databaseKey(),
81
			'translations' => $translations,
82
		];
83
	}
84
85
	/**
86
	 * @return string
87
	 */
88
	protected function normalizeSettingPath( $path )
89
	{
90
		return glsr( Helper::class )->prefixString( rtrim( $path, '.' ), 'settings.' );
91
	}
92
}
93