Passed
Branch master (bf85d9)
by Johannes
05:40
created

SerializationContext   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 0
Metric Value
dl 0
loc 115
ccs 44
cts 50
cp 0.88
rs 10
c 0
b 0
f 0
wmc 22

13 Methods

Rating   Name   Duplication   Size   Complexity  
A isVisiting() 0 7 2
A getDepth() 0 3 1
A initialize() 0 6 1
A setInitialType() 0 5 1
A getVisitingSet() 0 3 1
A create() 0 3 1
A getObject() 0 3 2
A getInitialType() 0 5 3
A getVisitingStack() 0 3 1
A startVisiting() 0 7 2
A getDirection() 0 3 1
A getPath() 0 12 3
A stopVisiting() 0 10 3
1
<?php
2
3
/*
4
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Serializer;
20
21
use JMS\Serializer\Exception\RuntimeException;
22
use Metadata\MetadataFactoryInterface;
23
24
class SerializationContext extends Context
25
{
26
    /** @var \SplObjectStorage */
27
    private $visitingSet;
28
29
    /** @var \SplStack */
30
    private $visitingStack;
31
32
    /**
33
     * @var string
34
     */
35
    private $initialType;
36
37 88
    public static function create()
38
    {
39 88
        return new self();
40
    }
41
42
    /**
43
     * @param string $format
44
     */
45 254
    public function initialize(string $format, $visitor, GraphNavigatorInterface $navigator, MetadataFactoryInterface $factory): void
46
    {
47 254
        parent::initialize($format, $visitor, $navigator, $factory);
48
49 254
        $this->visitingSet = new \SplObjectStorage();
50 254
        $this->visitingStack = new \SplStack();
51 254
    }
52
53 187
    public function startVisiting($object): void
54
    {
55 187
        if (!\is_object($object)) {
56 4
            return;
57
        }
58 183
        $this->visitingSet->attach($object);
59 183
        $this->visitingStack->push($object);
60 183
    }
61
62 179
    public function stopVisiting($object): void
63
    {
64 179
        if (!\is_object($object)) {
65 4
            return;
66
        }
67 175
        $this->visitingSet->detach($object);
68 175
        $poppedObject = $this->visitingStack->pop();
69
70 175
        if ($object !== $poppedObject) {
71
            throw new RuntimeException('Context visitingStack not working well');
72
        }
73 175
    }
74
75 187
    public function isVisiting($object): bool
76
    {
77 187
        if (!\is_object($object)) {
78 8
            return false;
79
        }
80
81 183
        return $this->visitingSet->contains($object);
82
    }
83
84 1
    public function getPath(): string
85
    {
86 1
        $path = array();
87 1
        foreach ($this->visitingStack as $obj) {
88 1
            $path[] = \get_class($obj);
89
        }
90
91 1
        if (!$path) {
92
            return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return string.
Loading history...
93
        }
94
95 1
        return implode(' -> ', $path);
96
    }
97
98 1
    public function getDirection(): int
99
    {
100 1
        return GraphNavigatorInterface::DIRECTION_SERIALIZATION;
101
    }
102
103 7
    public function getDepth(): int
104
    {
105 7
        return $this->visitingStack->count();
106
    }
107
108 23
    public function getObject()
109
    {
110 23
        return !$this->visitingStack->isEmpty() ? $this->visitingStack->top() : null;
111
    }
112
113
    public function getVisitingStack()
114
    {
115
        return $this->visitingStack;
116
    }
117
118
    public function getVisitingSet()
119
    {
120
        return $this->visitingSet;
121
    }
122
123
    /**
124
     * @param string $type
125
     * @return $this
126
     */
127 6
    public function setInitialType($type): self
128
    {
129 6
        $this->initialType = $type;
130 6
        $this->setAttribute('initial_type', $type);
131 6
        return $this;
132
    }
133
134 1
    public function getInitialType(): ?string
135
    {
136 1
        return $this->initialType
137 1
            ? $this->initialType
138 1
            : $this->hasAttribute('initial_type') ? $this->getAttribute('initial_type') : null;
139
    }
140
}
141