EnableByMatchingIdentityId::isSatisfiedBy()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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