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

EnableByMatchingIdentityId::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 7
cts 7
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 Segments $segments;
17
18 6
    public function __construct(Segments $segments)
19
    {
20 6
        foreach ($segments->all() as $segment) {
21 4
            if (false === $segment instanceof IdentitySegment) {
22 1
                throw new InvalidArgumentException(sprintf(
23 1
                    'Enable by matching identity id segment must be instance of %s class.',
24 1
                    IdentitySegment::class
25
                ));
26
            }
27
        }
28 5
        $this->segments = $segments;
29 5
    }
30
31 3
    public function isSatisfiedBy(ConsumerIdentity $identity): bool
32
    {
33 3
        foreach ($this->segments->all() as $segment) {
34 2
            if ($segment->match(['identity_id' => $identity->id()])) {
35 1
                return true;
36
            }
37
        }
38
39 2
        return false;
40
    }
41
42 1
    public function toArray(): array
43
    {
44
        return [
45 1
            'type' => self::NAME,
46 1
            'segments' => array_map(
47 1
                static fn(ISegment $segment): array => $segment->toArray(),
48 1
                $this->segments->all()
49
            ),
50
        ];
51
    }
52
53
    /**
54
     * @return array<string, string|array>
55
     */
56 1
    public function jsonSerialize(): array
57
    {
58 1
        return $this->toArray();
59
    }
60
}
61