Slot::addTarget()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace AdDirector\GPT;
4
5
use Illuminate\Support\Str;
6
7
/**
8
 * @link https://developers.google.com/publisher-tag/reference#googletag.defineSlot
9
 */
10
class Slot
11
{
12
    protected string $divId;
13
14
    protected array $targets = [];
15
16 5
    public function __construct(
17
        protected string      $adUnitPath,
18
        protected Size|string $size,
19
        ?string               $divId = null
20
    ) {
21 5
        if (is_null($divId)) {
22 3
            $divId = 'id_'.Str::random(8).'_'.time();
23
        }
24 5
        $this->divId = $divId;
25
    }
26
27 5
    public static function make(...$arguments): static
28
    {
29 5
        return new static(...$arguments);
30
    }
31
32 4
    public function targets(): array
33
    {
34 4
        return $this->targets;
35
    }
36
37 1
    public function addTarget(string $key, string|array $value): static
38
    {
39 1
        $this->targets[$key] = $value;
40
41 1
        return $this;
42
    }
43
44 1
    public function clearTargets(string|array|null $key = null): static
45
    {
46 1
        if (is_null($key)) {
47 1
            $this->targets = [];
48
49 1
            return $this;
50
        }
51
52 1
        foreach (((array) $key) as $key) {
53 1
            unset($this->targets[$key]);
54
        }
55
56 1
        return $this;
57
    }
58
59 4
    public function adUnitPath(): string
60
    {
61 4
        return $this->adUnitPath;
62
    }
63
64 4
    public function divId(): string
65
    {
66 4
        return $this->divId;
67
    }
68
69 4
    public function size(): Size|string
70
    {
71 4
        return $this->size;
72
    }
73
}
74