Passed
Push — master ( 265f32...0e6955 )
by Dominik
02:02 queued 12s
created

GroupPolicy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 48
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isCompliant() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Policy;
6
7
use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface;
8
9
final class GroupPolicy implements PolicyInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    const ATTRIBUTE_GROUPS = 'groups';
15
16
    /**
17
     * @var string
18
     */
19
    const GROUP_DEFAULT = 'default';
20
21
    /**
22
     * @var string[]
23
     */
24
    private $groups;
25
26
    /**
27
     * @param array $groups
28
     */
29 5
    public function __construct(array $groups = [self::GROUP_DEFAULT])
30
    {
31 5
        $this->groups = $groups;
32 5
    }
33
34
    /**
35
     * @param DenormalizerContextInterface $context
36
     * @param object|mixed                 $object
37
     *
38
     * @return bool
39
     */
40 5
    public function isCompliant(DenormalizerContextInterface $context, $object): bool
41
    {
42 5
        if ([] === $this->groups) {
43 1
            return true;
44
        }
45
46 4
        $contextGroups = $context->getAttribute(self::ATTRIBUTE_GROUPS, [self::GROUP_DEFAULT]);
47
48 4
        foreach ($this->groups as $group) {
49 4
            if (in_array($group, $contextGroups, true)) {
50 4
                return true;
51
            }
52
        }
53
54 2
        return false;
55
    }
56
}
57