Passed
Push — 1.0.x ( 582123...077e14 )
by Koldo
02:53
created

EnableByMatchingSegment::id()   A

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 8
    }
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
    /**
46
     * @return array<string, string|array>
47
     */
48 1
    public function toArray(): array
49
    {
50
        return [
51 1
            'id' => $this->id,
52 1
            'type' => self::NAME,
53 1
            'segments' => array_map(
54 1
                static fn(ISegment $segment): array => $segment->toArray(),
55 1
                $this->segments->all()
56
            ),
57
        ];
58
    }
59
60
    /**
61
     * @return array<string, string|array>
62
     */
63 1
    public function jsonSerialize(): array
64
    {
65 1
        return $this->toArray();
66
    }
67
}
68