Test Failed
Push — master ( 135f34...8b25c6 )
by Chris
33:49
created

SettingsField::getSettingData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leonidas\Library\Admin\Component\SettingsField;
4
5
use Leonidas\Contracts\Admin\Component\SettingsFieldInterface;
6
use Leonidas\Library\Admin\Abstracts\CanBeRestrictedTrait;
7
use Leonidas\Library\Admin\Component\SettingsField\Traits\HasSettingsFieldDataTrait;
8
use Psr\Http\Message\ServerRequestInterface;
9
use WebTheory\Html\Html;
10
use WebTheory\HttpPolicy\ServerRequestPolicyInterface;
11
use WebTheory\Saveyour\Contracts\Field\FormFieldInterface;
12
use WebTheory\Saveyour\Contracts\Formatting\DataFormatterInterface;
13
use WebTheory\Saveyour\Field\Type\Text;
14
15
class SettingsField implements SettingsFieldInterface
16
{
17
    use CanBeRestrictedTrait;
18
    use HasSettingsFieldDataTrait;
19
20
    public function __construct(
21
        string $setting,
22
        string $id,
23
        string $title,
24
        string $page,
25
        string $section,
26
        ?string $inputId = null,
27
        ?array $args = null,
28
        ?FormFieldInterface $input = null,
29
        ?DataFormatterInterface $formatter = null,
30
        ?ServerRequestPolicyInterface $policy = null
31
    ) {
32
        $this->id = $id;
33
        $this->title = $title;
34
        $this->page = $page;
35
        $this->section = $section;
36
        $this->setting = $setting;
37
38
        $this->inputId = $inputId ?? $this->id;
39
40
        $args && $this->args = $args;
41
        $input && $this->input = $input;
42
        $formatter && $this->formatter = $formatter;
43
        $policy && $this->policy = $policy;
44
    }
45
46
    public function renderComponent(ServerRequestInterface $request): string
47
    {
48
        $output = '';
49
        $settingData = $this->getSettingData();
50
        $value = get_option($this->getSetting(), $settingData['default'] ?? null);
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

50
        $value = /** @scrutinizer ignore-call */ get_option($this->getSetting(), $settingData['default'] ?? null);
Loading history...
51
52
        $output .= ($this->input ?? $this->getDefaultInput())
53
            ->setName($this->getSetting())
54
            ->setValue($this->formatValue($value))
55
            ->setId($this->getInputId())
56
            ->toHtml() . "\n";
57
58
        if ($description = $settingData['description'] ?? false) {
59
            $output .= $this->renderDescription($description) . "\n";
60
        }
61
62
        return $output;
63
    }
64
65
    protected function getSettingData(): array
66
    {
67
        return get_registered_settings()[$this->getSetting()];
0 ignored issues
show
Bug introduced by
The function get_registered_settings was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

67
        return /** @scrutinizer ignore-call */ get_registered_settings()[$this->getSetting()];
Loading history...
68
    }
69
70
    protected function getDefaultInput(): FormFieldInterface
71
    {
72
        $input = new Text();
73
        $input->addClass('regular-text');
74
75
        return $input;
76
    }
77
78
    protected function renderDescription($description)
79
    {
80
        $this->args['description_attr']['class'][] = 'description';
81
82
        return Html::tag('p', [$this->args['description_attr']], $description);
83
    }
84
85
    protected function formatValue($value)
86
    {
87
        return $this->formatter ? $this->formatter->formatData($value) : $value;
88
    }
89
}
90