EnableByMatchingSegment::id()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pheature\Model\Toggle;
6
7
use Pheature\Core\Toggle\Read\ConsumerIdentity;
8
use Pheature\Core\Toggle\Read\Segment as ISegment;
9
use Pheature\Core\Toggle\Read\Segments;
10
use Pheature\Core\Toggle\Read\ToggleStrategy;
11
12
final class EnableByMatchingSegment implements ToggleStrategy
13
{
14
    public const NAME = 'enable_by_matching_segment';
15
    private string $id;
16
    private Segments $segments;
17
18 8
    public function __construct(string $id, Segments $segments)
19
    {
20 8
        $this->id = $id;
21 8
        $this->segments = $segments;
22
    }
23
24 3
    public function id(): string
25
    {
26 3
        return $this->id;
27
    }
28
29 3
    public function type(): string
30
    {
31 3
        return self::NAME;
32
    }
33
34 6
    public function isSatisfiedBy(ConsumerIdentity $identity): bool
35
    {
36 6
        foreach ($this->segments->all() as $segment) {
37 5
            if ($segment->match($identity->payload())) {
38 3
                return true;
39
            }
40
        }
41
42 3
        return false;
43
    }
44
45 1
    public function toArray(): array
46
    {
47
        return [
48 1
            'id' => $this->id,
49
            'type' => self::NAME,
50 1
            'segments' => array_map(
51 1
                static fn(ISegment $segment): array => $segment->toArray(),
52 1
                $this->segments->all()
53
            ),
54
        ];
55
    }
56
57 1
    public function jsonSerialize(): array
58
    {
59 1
        return $this->toArray();
60
    }
61
}
62