GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Manager   A
last analyzed

Coupling/Cohesion

Components 2
Dependencies 3

Complexity

Total Complexity 26

Size/Duplication

Total Lines 294
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 294
ccs 86
cts 86
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A trimToAcceptableRecursionLevel() 0 4 1
A createData() 0 15 2
A getIncludeParams() 0 6 2
A getRequestedIncludes() 0 4 1
A getRequestedExcludes() 0 4 1
A getSerializer() 0 8 2
B parseIncludes() 0 59 7
B parseExcludes() 0 26 5
A setRecursionLimit() 0 6 1
A setSerializer() 0 6 1
A autoIncludeParents() 0 18 3
1
<?php
2
3
/*
4
 * This file is part of the League\Fractal package.
5
 *
6
 * (c) Phil Sturgeon <[email protected]>
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 League\Fractal;
13
14
use League\Fractal\Resource\ResourceInterface;
15
use League\Fractal\Serializer\DataArraySerializer;
16
use League\Fractal\Serializer\SerializerAbstract;
17
18
/**
19
 * Manager
20
 *
21
 * Not a wildly creative name, but the manager is what a Fractal user will interact
22
 * with the most. The manager has various configurable options, and allows users
23
 * to create the "root scope" easily.
24
 */
25
class Manager
26
{
27
    /**
28
     * Array of scope identifiers for resources to include.
29
     *
30
     * @var array
31
     */
32
    protected $requestedIncludes = [];
33
34
    /**
35
     * Array of scope identifiers for resources to exclude.
36
     *
37
     * @var array
38
     */
39
    protected $requestedExcludes = [];
40
41
    /**
42
     * Array containing modifiers as keys and an array value of params.
43
     *
44
     * @var array
45
     */
46
    protected $includeParams = [];
47
48
    /**
49
     * The character used to separate modifier parameters.
50
     *
51
     * @var string
52
     */
53
    protected $paramDelimiter = '|';
54
55
    /**
56
     * Upper limit to how many levels of included data are allowed.
57
     *
58
     * @var int
59
     */
60
    protected $recursionLimit = 10;
61
62
    /**
63
     * Serializer.
64
     *
65
     * @var SerializerAbstract
66
     */
67
    protected $serializer;
68
69
    /**
70
     * Create Data.
71
     *
72
     * Main method to kick this all off. Make a resource then pass it over, and use toArray()
73
     *
74
     * @param ResourceInterface $resource
75
     * @param string            $scopeIdentifier
76
     * @param Scope             $parentScopeInstance
77
     *
78
     * @return Scope
79
     */
80 35
    public function createData(ResourceInterface $resource, $scopeIdentifier = null, Scope $parentScopeInstance = null)
81
    {
82 35
        $scopeInstance = new Scope($this, $resource, $scopeIdentifier);
83
84
        // Update scope history
85 35
        if ($parentScopeInstance !== null) {
86
            // This will be the new children list of parents (parents parents, plus the parent)
87 30
            $scopeArray = $parentScopeInstance->getParentScopes();
88 30
            $scopeArray[] = $parentScopeInstance->getScopeIdentifier();
89
90 30
            $scopeInstance->setParentScopes($scopeArray);
91 30
        }
92
93 35
        return $scopeInstance;
94
    }
95
96
    /**
97
     * Get Include Params.
98
     *
99
     * @param string $include
100
     *
101
     * @return \League\Fractal\ParamBag
102
     */
103 26
    public function getIncludeParams($include)
104
    {
105 26
        $params = isset($this->includeParams[$include]) ? $this->includeParams[$include] : [];
106
107 26
        return new ParamBag($params);
108
    }
109
110
    /**
111
     * Get Requested Includes.
112
     *
113
     * @return array
114
     */
115 38
    public function getRequestedIncludes()
116
    {
117 38
        return $this->requestedIncludes;
118
    }
119
120
    /**
121
     * Get Requested Excludes.
122
     *
123
     * @return array
124
     */
125 27
    public function getRequestedExcludes()
126
    {
127 27
        return $this->requestedExcludes;
128
    }
129
130
    /**
131
     * Get Serializer.
132
     *
133
     * @return SerializerAbstract
134
     */
135 46
    public function getSerializer()
136
    {
137 46
        if (! $this->serializer) {
138 7
            $this->setSerializer(new DataArraySerializer());
139 7
        }
140
141 46
        return $this->serializer;
142
    }
143
144
    /**
145
     * Parse Include String.
146
     *
147
     * @param array|string $includes Array or csv string of resources to include
148
     *
149
     * @return $this
150
     */
151 37
    public function parseIncludes($includes)
152
    {
153
        // Wipe these before we go again
154 37
        $this->requestedIncludes = $this->includeParams = [];
155
156 37
        if (is_string($includes)) {
157 31
            $includes = explode(',', $includes);
158 31
        }
159
160 37
        if (! is_array($includes)) {
161 2
            throw new \InvalidArgumentException(
162 2
                'The parseIncludes() method expects a string or an array. '.gettype($includes).' given'
163 2
            );
164
        }
165
166 35
        foreach ($includes as $include) {
167 35
            list($includeName, $allModifiersStr) = array_pad(explode(':', $include, 2), 2, null);
168
169
            // Trim it down to a cool level of recursion
170 35
            $includeName = $this->trimToAcceptableRecursionLevel($includeName);
171
172 35
            if (in_array($includeName, $this->requestedIncludes)) {
173 1
                continue;
174
            }
175 35
            $this->requestedIncludes[] = $includeName;
176
177
            // No Params? Bored
178 35
            if ($allModifiersStr === null) {
179 34
                continue;
180
            }
181
182
            // Matches multiple instances of 'something(foo|bar|baz)' in the string
183
            // I guess it ignores : so you could use anything, but probably don't do that
184 2
            preg_match_all('/([\w]+)(\(([^\)]+)\))?/', $allModifiersStr, $allModifiersArr);
185
186
            // [0] is full matched strings...
187 2
            $modifierCount = count($allModifiersArr[0]);
188
189 2
            $modifierArr = [];
190
191 2
            for ($modifierIt = 0; $modifierIt < $modifierCount; $modifierIt++) {
192
                // [1] is the modifier
193 2
                $modifierName = $allModifiersArr[1][$modifierIt];
194
195
                // and [3] is delimited params
196 2
                $modifierParamStr = $allModifiersArr[3][$modifierIt];
197
198
                // Make modifier array key with an array of params as the value
199 2
                $modifierArr[$modifierName] = explode($this->paramDelimiter, $modifierParamStr);
200 2
            }
201
202 2
            $this->includeParams[$includeName] = $modifierArr;
203 35
        }
204
205
        // This should be optional and public someday, but without it includes would never show up
206 35
        $this->autoIncludeParents();
207
208 35
        return $this;
209
    }
210
211
    /**
212
     * Parse Exclude String.
213
     *
214
     * @param array|string $excludes Array or csv string of resources to exclude
215
     *
216
     * @return $this
217
     */
218 5
    public function parseExcludes($excludes)
219
    {
220 5
        $this->requestedExcludes = [];
221
222 5
        if (is_string($excludes)) {
223 2
            $excludes = explode(',', $excludes);
224 2
        }
225
226 5
        if (! is_array($excludes)) {
227 2
            throw new \InvalidArgumentException(
228 2
                'The parseExcludes() method expects a string or an array. '.gettype($excludes).' given'
229 2
            );
230
        }
231
232 3
        foreach ($excludes as $excludeName) {
233 3
            $excludeName = $this->trimToAcceptableRecursionLevel($excludeName);
234
235 3
            if (in_array($excludeName, $this->requestedExcludes)) {
236 1
                continue;
237
            }
238
239 3
            $this->requestedExcludes[] = $excludeName;
240 3
        }
241
242 3
        return $this;
243
    }
244
245
    /**
246
     * Set Recursion Limit.
247
     *
248
     * @param int $recursionLimit
249
     *
250
     * @return $this
251
     */
252 1
    public function setRecursionLimit($recursionLimit)
253
    {
254 1
        $this->recursionLimit = $recursionLimit;
255
256 1
        return $this;
257
    }
258
259
    /**
260
     * Set Serializer
261
     *
262
     * @param SerializerAbstract $serializer
263
     *
264
     * @return $this
265
     */
266 46
    public function setSerializer(SerializerAbstract $serializer)
267
    {
268 46
        $this->serializer = $serializer;
269
270 46
        return $this;
271
    }
272
273
    /**
274
     * Auto-include Parents
275
     *
276
     * Look at the requested includes and automatically include the parents if they
277
     * are not explicitly requested. E.g: [foo, bar.baz] becomes [foo, bar, bar.baz]
278
     *
279
     * @internal
280
     *
281
     * @return void
282
     */
283 35
    protected function autoIncludeParents()
284
    {
285 35
        $parsed = [];
286
287 35
        foreach ($this->requestedIncludes as $include) {
288 35
            $nested = explode('.', $include);
289
290 35
            $part = array_shift($nested);
291 35
            $parsed[] = $part;
292
293 35
            while (count($nested) > 0) {
294 7
                $part .= '.'.array_shift($nested);
295 7
                $parsed[] = $part;
296 7
            }
297 35
        }
298
299 35
        $this->requestedIncludes = array_values(array_unique($parsed));
300 35
    }
301
302
    /**
303
     * Trim to Acceptable Recursion Level
304
     *
305
     * Strip off any requested resources that are too many levels deep, to avoid DiCaprio being chased
306
     * by trains or whatever the hell that movie was about.
307
     *
308
     * @internal
309
     *
310
     * @param string $includeName
311
     *
312
     * @return string
313
     */
314 37
    protected function trimToAcceptableRecursionLevel($includeName)
315
    {
316 37
        return implode('.', array_slice(explode('.', $includeName), 0, $this->recursionLimit));
317
    }
318
}
319