Completed
Push — master ( a2b1ad...0eab53 )
by Eric
02:58
created

MaxDepthExclusionStrategy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 0
cbo 3
dl 0
loc 51
ccs 19
cts 20
cp 0.95
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A skipClass() 0 4 1
A skipType() 0 4 1
B skip() 0 27 6
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Exclusion;
13
14
use Ivory\Serializer\Context\ContextInterface;
15
use Ivory\Serializer\Mapping\ClassMetadataInterface;
16
use Ivory\Serializer\Mapping\PropertyMetadataInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class MaxDepthExclusionStrategy extends ExclusionStrategy
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 9
    public function skipClass(ClassMetadataInterface $class, ContextInterface $context)
28
    {
29 9
        return $this->skip($context);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 9
    public function skipType(TypeMetadataInterface $type, ContextInterface $context)
36
    {
37 9
        return $this->skip($context);
38
    }
39
40
    /**
41
     * @param ContextInterface $context
42
     *
43
     * @return bool
44
     */
45 9
    private function skip(ContextInterface $context)
46
    {
47 9
        $depth = 0;
48 9
        $dataDepth = count($context->getDataStack());
49
50 9
        foreach ($context->getMetadataStack() as $metadata) {
51 9
            if ($metadata instanceof TypeMetadataInterface) {
52 9
                ++$depth;
53 6
            }
54
55 9
            if (!$metadata instanceof PropertyMetadataInterface) {
56 9
                continue;
57
            }
58
59 9
            ++$depth;
60
61 9
            if (!$metadata->hasMaxDepth()) {
62
                continue;
63
            }
64
65 9
            if ($dataDepth - $depth > $metadata->getMaxDepth()) {
66 9
                return true;
67
            }
68 6
        }
69
70 9
        return false;
71
    }
72
}
73