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

MaxDepthExclusionStrategy::skip()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
rs 8.439
cc 6
eloc 14
nc 9
nop 1
crap 6.0087
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