Completed
Pull Request — master (#1519)
by Christian
07:43
created

Context::disableMaxDepth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Context;
13
14
/**
15
 * Stores the serialization or deserialization context (groups, version, ...).
16
 *
17
 * @author Ener-Getick <[email protected]>
18
 */
19
final class Context
20
{
21
    /**
22
     * @var array
23
     */
24
    private $attributes = array();
25
    /**
26
     * @var int|null
27
     */
28
    private $version;
29
    /**
30
     * @var array
31
     */
32
    private $groups = array();
33
    /**
34
     * @var int
35
     */
36
    private $maxDepth;
37
    /**
38
     * @var bool
39
     */
40
    private $isMaxDepthEnabled;
41
    /**
42
     * @var bool
43
     */
44
    private $serializeNull;
45
46
    /**
47
     * Sets an attribute.
48
     *
49
     * @param string $key
50
     * @param mixed  $value
51
     *
52
     * @return self
53
     */
54 29
    public function setAttribute($key, $value)
55
    {
56 29
        $this->attributes[$key] = $value;
57
58 29
        return $this;
59
    }
60
61
    /**
62
     * Checks if contains a normalization attribute.
63
     *
64
     * @param string $key
65
     *
66
     * @return bool
67
     */
68 1
    public function hasAttribute($key)
69
    {
70 1
        return isset($this->attributes[$key]);
71
    }
72
73
    /**
74
     * Gets an attribute.
75
     *
76
     * @param string $key
77
     *
78
     * @return mixed
79
     */
80 2
    public function getAttribute($key)
81
    {
82 2
        if (isset($this->attributes[$key])) {
83 2
            return $this->attributes[$key];
84
        }
85
    }
86
87
    /**
88
     * Gets the attributes.
89
     *
90
     * @return array
91
     */
92 18
    public function getAttributes()
93
    {
94 18
        return $this->attributes;
95
    }
96
97
    /**
98
     * Sets the normalization version.
99
     *
100
     * @param int|null $version
101
     *
102
     * @return self
103
     */
104 6
    public function setVersion($version)
105
    {
106 6
        $this->version = $version;
107
108 6
        return $this;
109
    }
110
111
    /**
112
     * Gets the normalization version.
113
     *
114
     * @return int|null
115
     */
116 33
    public function getVersion()
117
    {
118 33
        return $this->version;
119
    }
120
121
    /**
122
     * Adds a normalization group.
123
     *
124
     * @param string $group
125
     *
126
     * @return self
127
     */
128 6
    public function addGroup($group)
129
    {
130 6
        if (!in_array($group, $this->groups)) {
131 6
            $this->groups[] = $group;
132 6
        }
133
134 6
        return $this;
135
    }
136
137
    /**
138
     * Adds normalization groups.
139
     *
140
     * @param string[] $groups
141
     *
142
     * @return self
143
     */
144 5
    public function addGroups(array $groups)
145
    {
146 5
        foreach ($groups as $group) {
147 5
            $this->addGroup($group);
148 5
        }
149
150 5
        return $this;
151
    }
152
153
    /**
154
     * Gets the normalization groups.
155
     *
156
     * @return string[]
157
     */
158 36
    public function getGroups()
159
    {
160 36
        return $this->groups;
161
    }
162
163
    /**
164
     * Set the normalization groups.
165
     *
166
     * @param string[] $groups
167
     *
168
     * @return self
169
     */
170 1
    public function setGroups(array $groups)
171
    {
172 1
        $this->groups = $groups;
173
174 1
        return $this;
175
    }
176
177
    /**
178
     * Sets the normalization max depth.
179
     *
180
     * @param int|null $maxDepth
181
     *
182
     * @return self
183
     *
184
     * @deprecated since 2.1, to be removed in 3.0. Use {@link self::enableMaxDepth()} and {@link self::disableMaxDepth()} instead.
185
     */
186 3 View Code Duplication
    public function setMaxDepth($maxDepth)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188 3
        if (1 === func_num_args() || func_get_arg(1)) {
189 2
            @trigger_error(sprintf('%s is deprecated since version 2.1 and will be removed in 3.0. Use %s::enableMaxDepth() and %s::disableMaxDepth() instead.', __METHOD__, __CLASS__, __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
190 2
        }
191 3
        $this->maxDepth = $maxDepth;
192
193 3
        return $this;
194
    }
195
196
    /**
197
     * Gets the normalization max depth.
198
     *
199
     * @return int|null
200
     *
201
     * @deprecated since version 2.1, to be removed in 3.0. Use {@link self::isMaxDepthEnabled()} instead.
202
     */
203 19 View Code Duplication
    public function getMaxDepth()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205 19
        if (0 === func_num_args() || func_get_arg(0)) {
206 1
            @trigger_error(sprintf('%s is deprecated since version 2.1 and will be removed in 3.0. Use %s::isMaxDepthEnabled() instead.', __METHOD__, __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
207 1
        }
208
209 19
        return $this->maxDepth;
210
    }
211
212 2
    public function enableMaxDepth()
213
    {
214 2
        $this->isMaxDepthEnabled = true;
215
216 2
        return $this;
217
    }
218
219 1
    public function disableMaxDepth()
220
    {
221 1
        $this->isMaxDepthEnabled = false;
222
223 1
        return $this;
224
    }
225
226
    /**
227
     * @return bool|null
228
     */
229 18
    public function isMaxDepthEnabled()
230
    {
231 18
        return $this->isMaxDepthEnabled;
232
    }
233
234
    /**
235
     * Sets serialize null.
236
     *
237
     * @param bool|null $serializeNull
238
     *
239
     * @return self
240
     */
241 19
    public function setSerializeNull($serializeNull)
242
    {
243 19
        $this->serializeNull = $serializeNull;
244
245 19
        return $this;
246
    }
247
248
    /**
249
     * Gets serialize null.
250
     *
251
     * @return bool|null
252
     */
253 31
    public function getSerializeNull()
254
    {
255 31
        return $this->serializeNull;
256
    }
257
}
258