Test Failed
Push — master ( 2c2c87...ed1c51 )
by Chris
25:05 queued 37s
created

SettingsFieldLoader::registerMany()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Leonidas\Library\Admin\Page\SettingsField;
4
5
use Leonidas\Contracts\Admin\Components\SettingsFieldCollectionInterface;
6
use Leonidas\Contracts\Admin\Components\SettingsFieldInterface;
7
use Leonidas\Contracts\Admin\Components\SettingsFieldLoaderInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
class SettingsFieldLoader implements SettingsFieldLoaderInterface
11
{
12
    /**
13
     * @var callable
14
     */
15
    protected $outputHandler;
16
17
    public function __construct(callable $outputHandler)
18
    {
19
        $this->outputHandler = $outputHandler;
20
    }
21
22
    public function getOutputHandler(): callable
23
    {
24
        return $this->outputHandler;
25
    }
26
27
    public function registerOne(SettingsFieldInterface $field, ServerRequestInterface $request)
28
    {
29
        if ($field->shouldBeRendered($request)) {
30
            add_settings_field(
0 ignored issues
show
Bug introduced by
The function add_settings_field 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

30
            /** @scrutinizer ignore-call */ 
31
            add_settings_field(
Loading history...
31
                $field->getId(),
32
                $field->getTitle(),
33
                $this->getOutputHandler(),
34
                $field->getPage(),
35
                $field->getSection(),
36
                $this->getFieldArgs($field)
37
            );
38
        }
39
    }
40
41
    public function registerMany(SettingsFieldCollectionInterface $fields, ServerRequestInterface $request)
42
    {
43
        foreach ($fields->all() as $field) {
44
            $this->registerOne($field, $request);
45
        }
46
    }
47
48
    protected function getFieldArgs(SettingsFieldInterface $field): array
49
    {
50
        return [
51
            '@base' => $field,
52
            'label_for' => $field->getInputId(),
53
        ] + $field->getArgs();
54
    }
55
}
56