Passed
Push — 1.0.x ( 54a243...6cfe9a )
by Koldo
02:32
created

EnableByMatchingSegment   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
dl 0
loc 41
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 1
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 7 1
A jsonSerialize() 0 3 1
A __construct() 0 3 1
A isSatisfiedBy() 0 9 3
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 Segments $segments;
16
17 8
    public function __construct(Segments $segments)
18
    {
19 8
        $this->segments = $segments;
20 8
    }
21
22 6
    public function isSatisfiedBy(ConsumerIdentity $identity): bool
23
    {
24 6
        foreach ($this->segments->all() as $segment) {
25 5
            if ($segment->match($identity->payload())) {
26 3
                return true;
27
            }
28
        }
29
30 3
        return false;
31
    }
32
33
    /**
34
     * @return array<string, string|array>
35
     */
36 1
    public function toArray(): array
37
    {
38
        return [
39 1
            'type' => self::NAME,
40 1
            'segments' => array_map(
41 1
                static fn(ISegment $segment): array => $segment->toArray(),
42 1
                $this->segments->all()
43
            ),
44
        ];
45
    }
46
47
    /**
48
     * @return array<string, string|array>
49
     */
50 1
    public function jsonSerialize(): array
51
    {
52 1
        return $this->toArray();
53
    }
54
}
55