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

CircularReferenceExclusionStrategy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 46
ccs 15
cts 20
cp 0.75
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C skipClass() 0 27 7
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
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class CircularReferenceExclusionStrategy extends ExclusionStrategy
21
{
22
    /**
23
     * @var int
24
     */
25
    private $limit;
26
27
    /**
28
     * @param int $limit
29
     */
30
    public function __construct($limit = 1)
31
    {
32
        $this->limit = $limit;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 9
    public function skipClass(ClassMetadataInterface $class, ContextInterface $context)
39
    {
40 9
        $references = [];
41 9
        $dataStack = $context->getDataStack();
42
43 9
        foreach ($context->getMetadataStack() as $depth => $metadata) {
44 9
            $data = $dataStack[$depth];
45
46 9
            if (!$metadata instanceof ClassMetadataInterface || !is_object($data)) {
47
                continue;
48
            }
49
50 9
            $hash = spl_object_hash($data);
51
52 9
            if (!isset($references[$hash])) {
53 9
                $references[$hash] = 0;
54 6
            }
55
56 9
            foreach ($references as &$reference) {
57 9
                if (++$reference >= $this->limit) {
58 9
                    return true;
59
                }
60
            }
61 6
        }
62
63 9
        return false;
64
    }
65
}
66