Passed
Push — master ( 796b78...de3336 )
by Paul
05:32
created

Form   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeSettingPath() 0 3 1
A renderFields() 0 3 1
A getTemplateContext() 0 10 2
A getSettingFields() 0 6 1
A buildFields() 0 8 2
A getTemplateContextForTranslations() 0 10 2
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 Form
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 $id
31
	 * @return void
32
	 */
33
	public function renderFields( $id )
34
	{
35
		echo $this->buildFields( $id );
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, ['name' => $name] );
59
			$rows.= (new Field( $field ))->build();
60
		}
61
		return [
62
			'rows' => $rows,
63
		];
64
	}
65
66
	/**
67
	 * @return array
68
	 */
69
	protected function getTemplateContextForTranslations()
70
	{
71
		$translations = glsr( Translator::class )->renderAll();
72
		$class = empty( $translations )
73
			? 'glsr-hidden'
74
			: '';
75
		return [
76
			'class' => $class,
77
			'database_key' => OptionManager::databaseKey(),
78
			'translations' => $translations,
79
		];
80
	}
81
82
	/**
83
	 * @return string
84
	 */
85
	protected function normalizeSettingPath( $path )
86
	{
87
		return glsr( Helper::class )->prefixString( rtrim( $path, '.' ), 'settings.' );
88
	}
89
}
90