Passed
Push — 1.0.x ( 16fd64...da3a7c )
by Koldo
02:35
created

StrictMatchingSegment   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 70
ccs 15
cts 21
cp 0.7143
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 3 1
A jsonSerialize() 0 3 1
A criteria() 0 3 1
A match() 0 17 4
A __construct() 0 4 1
A toArray() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pheature\Model\Toggle;
6
7
use Pheature\Core\Toggle\Read\Segment as ISegment;
8
9
final class StrictMatchingSegment implements ISegment
10
{
11
    public const NAME = 'strict_matching_segment';
12
    private string $id;
13
    /**
14
     * @var array<string, mixed>
15
     */
16
    private array $criteria;
17
18
    /**
19
     * Segment constructor.
20
     *
21
     * @param string               $id
22
     * @param array<string, mixed> $criteria
23
     */
24 8
    public function __construct(string $id, array $criteria)
25
    {
26 8
        $this->id = $id;
27 8
        $this->criteria = $criteria;
28 8
    }
29
30
    public function id(): string
31
    {
32
        return $this->id;
33
    }
34
35
    /**
36
     * @return array<string, mixed>
37
     */
38
    public function criteria(): array
39
    {
40
        return $this->criteria;
41
    }
42
43 5
    public function match(array $payload): bool
44
    {
45 5
        $match = false;
46
47
        /**
48
         * @var mixed $value
49
         */
50 5
        foreach ($this->criteria as $key => $value) {
51 4
            if (array_key_exists($key, $payload) && $value === $payload[$key]) {
52 3
                $match = true;
53 3
                continue;
54
            }
55
56 1
            return false;
57
        }
58
59 4
        return $match;
60
    }
61
62
    /**
63
     * @return array<string, string|array>
64
     */
65 1
    public function toArray(): array
66
    {
67
        return [
68 1
            'id' => $this->id,
69 1
            'criteria' => $this->criteria,
70
        ];
71
    }
72
73
    /**
74
     * @return array<string, string|array>
75
     */
76
    public function jsonSerialize(): array
77
    {
78
        return $this->toArray();
79
    }
80
}
81