Passed
Pull Request — master (#1200)
by Rene
03:53 queued 45s
created

DeserializationContext   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 55
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A removePersistentCollectionForCurrentPath() 0 9 2
A increaseDepth() 0 3 1
A getDepth() 0 3 1
A getDirection() 0 3 1
A addPersistentCollection() 0 3 1
A decreaseDepth() 0 7 2
A create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer;
6
7
use Doctrine\ORM\PersistentCollection;
8
use JMS\Serializer\Exception\LogicException;
9
10
class DeserializationContext extends Context
11
{
12
    /**
13 5
     * @var int
14
     */
15 5
    private $depth = 0;
16
17
    /**
18 1
     * @var array<string, PersistentCollection>
19
     */
20 1
    private $persistentCollections = [];
21
22
    public static function create(): self
23 6
    {
24
        return new self();
25 6
    }
26
27
    public function getDirection(): int
28 80
    {
29
        return GraphNavigatorInterface::DIRECTION_DESERIALIZATION;
30 80
    }
31 80
32
    public function getDepth(): int
33 77
    {
34
        return $this->depth;
35 77
    }
36
37
    public function increaseDepth(): void
38
    {
39 77
        $this->depth += 1;
40 77
    }
41
42
    public function decreaseDepth(): void
43
    {
44
        if ($this->depth <= 0) {
45
            throw new LogicException('Depth cannot be smaller than zero.');
46
        }
47
48
        $this->depth -= 1;
49
    }
50
51
    public function addPersistentCollection(PersistentCollection $collection, array $path): void
52
    {
53
        $this->persistentCollections[implode('.', $path)] = $collection;
54
    }
55
56
    public function removePersistentCollectionForCurrentPath(): ?PersistentCollection
57
    {
58
        $path = implode('.', $this->getCurrentPath());
59
        if (isset($this->persistentCollections[$path])) {
60
           $return = $this->persistentCollections[$path];
61
           unset($this->persistentCollections[$path]);
62
           return $return;
63
        }
64
        return null;
65
    }
66
}
67