Passed
Push — master ( 77e7d3...37f589 )
by Yaroslav
02:54
created

getFlexibleTargetsAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NovaAdDirector\Nova\Flexible\Layouts;
4
5
use AdDirector\Facades\AdDirector;
6
use AdDirector\GPT\AdGPT;
7
use AdDirector\GPT\Slot;
8
use Illuminate\Validation\Rule;
9
use Laravel\Nova\Fields\Text;
10
use NovaAdDirector\Contracts\ConfigurationLayout;
11
use NovaFlexibleContent\Concerns\HasFlexible;
12
use NovaFlexibleContent\Flexible;
13
use NovaFlexibleContent\Layouts\Layout;
14
use NovaFlexibleContent\Layouts\Preset;
15
16
class GPTConfigurationLayout extends Layout implements ConfigurationLayout
17
{
18
    use HasFlexible;
19
20
    protected string $name = 'gpt';
21
22
    protected string $title = 'gpt';
23
24
    public function title(): string
25
    {
26
        return trans("nova-ad-director::resource.layouts.{$this->title}");
27
    }
28
29
    protected function targetsPreset()
30
    {
31
        return Preset::withLayouts([
32
            GPTTarget::class,
33
        ]);
34
    }
35
36
    public function fields(): array
37
    {
38
        $allowedSizeKeys = array_keys(AdGPT::getSizes());
39
40
        return [
41
            Text::make(
42
                trans("nova-ad-director::resource.configuration.{$this->name}.adUnitPath"),
43
                'adUnitPath'
44
            )
45
                ->placeholder('/1234567/Example_Leaderboard')
46
                ->rules('required'),
47
            Text::make(
48
                trans("nova-ad-director::resource.configuration.{$this->name}.size"),
49
                'size'
50
            )
51
                ->placeholder($allowedSizeKeys[0] ?? trans("nova-ad-director::resource.configuration.{$this->name}.size"))
0 ignored issues
show
Bug introduced by
It seems like $allowedSizeKeys[0] ?? t....'.$this->name.'.size') can also be of type array and array; however, parameter $text of Laravel\Nova\Fields\Field::placeholder() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

51
                ->placeholder(/** @scrutinizer ignore-type */ $allowedSizeKeys[0] ?? trans("nova-ad-director::resource.configuration.{$this->name}.size"))
Loading history...
52
                ->rules('required', Rule::in($allowedSizeKeys))
53
                ->help(trans("nova-ad-director::resource.configuration.{$this->name}.help_size", [
0 ignored issues
show
Bug introduced by
It seems like trans('nova-ad-director:...llowedSizeKeys) : '-')) can also be of type array and array; however, parameter $text of Laravel\Nova\Fields\Field::help() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

53
                ->help(/** @scrutinizer ignore-type */ trans("nova-ad-director::resource.configuration.{$this->name}.help_size", [
Loading history...
54
                    'sizes' => !empty($allowedSizeKeys) ? implode(', ', $allowedSizeKeys) : '-',
55
                ])),
56
            Text::make(
57
                trans("nova-ad-director::resource.configuration.{$this->name}.divId"),
58
                'divId'
59
            )
60
                ->placeholder('div-gpt-ad-1234567891234-0')
61
                ->rules('nullable')
62
                ->help(trans("nova-ad-director::resource.configuration.{$this->name}.help_divId")),
63
            Flexible::make('Targets', 'targets')
64
                    ->preset($this->targetsPreset())
65
                    ->button('Add target'),
66
        ];
67
    }
68
69
    public function getFlexibleTargetsAttribute()
70
    {
71
        return $this->flexible('targets', $this->targetsPreset()->layouts());
72
    }
73
74
    public function applyConfiguration(string $locationName): static
75
    {
76
        $slot = Slot::make(
77
            $this->adUnitPath,
78
            $this->size,
79
            $this->divId ?: null
80
        );
81
82
        if ($this->flexibleTargets) {
83
            /**
84
             * @var \NovaAdDirector\Nova\Flexible\Layouts\GPTTarget $target
85
             */
86
            foreach ($this->targets as $target) {
87
                if (($key = trim($target->target_key))
88
                    && ($value = trim($target->target_value))) {
89
                    $values = array_filter(array_map('trim', explode(',', $value)));
90
                    if (!empty($values)) {
91
                        $slot->addTarget($key, $values);
92
                    }
93
                }
94
            }
95
        }
96
97
        AdDirector::gpt()->addLocation($slot, $locationName);
98
99
        return $this;
100
    }
101
}
102