HasTemplate::selectedTemplateFields()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace NovaThinKit\Nova\ResourceTemplates;
4
5
use Laravel\Nova\Fields\Select;
6
use Laravel\Nova\Http\Requests\NovaRequest;
7
8
trait HasTemplate
9
{
10 7
    public function templateKeyName(): string
11
    {
12 7
        return 'template';
13
    }
14 7
    protected function templateFields(NovaRequest $request): array
15
    {
16 7
        return [
17 7
            Select::make('Template', $this->templateKeyName())
18 7
                ->placeholder('Default')
19 7
                ->searchable()
20 7
                ->options(TemplateFinder::templatesNames(static::$model))
21 7
                ->displayUsingLabels()
22 7
                ->nullable(),
23 7
            ...$this->selectedTemplateFields($request),
24 7
        ];
25
    }
26
27 7
    protected function selectedTemplateFields(NovaRequest $request): array
28
    {
29 7
        if ($this->resource->{$this->templateKeyName()}) {
30 2
            return TemplateFinder::find($this->resource->{$this->templateKeyName()}, $this)?->fields($request) ?: [];
0 ignored issues
show
Bug introduced by
$this of type NovaThinKit\Nova\ResourceTemplates\HasTemplate is incompatible with the type Laravel\Nova\Resource expected by parameter $resource of NovaThinKit\Nova\Resourc...\TemplateFinder::find(). ( Ignorable by Annotation )

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

30
            return TemplateFinder::find($this->resource->{$this->templateKeyName()}, /** @scrutinizer ignore-type */ $this)?->fields($request) ?: [];
Loading history...
31
        }
32
33 5
        return [];
34
    }
35
}
36