EnableByMatchingSegment   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
dl 0
loc 48
ccs 19
cts 19
cp 1
rs 10
c 1
b 0
f 1
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 3 1
A type() 0 3 1
A __construct() 0 4 1
A isSatisfiedBy() 0 9 3
A toArray() 0 8 1
A jsonSerialize() 0 3 1
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