Completed
Branch master (91796e)
by
unknown
04:43
created

Factory::createParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.3143
cc 1
eloc 18
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php namespace Neomerx\JsonApi\Factories;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
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
use \Closure;
20
use \Neomerx\JsonApi\Schema\Link;
21
use \Neomerx\JsonApi\Document\Error;
22
use \Neomerx\JsonApi\Encoder\Encoder;
23
use \Neomerx\JsonApi\Schema\Container;
24
use \Neomerx\JsonApi\Document\Document;
25
use \Neomerx\JsonApi\Codec\CodecMatcher;
26
use \Neomerx\JsonApi\Encoder\Stack\Stack;
27
use \Neomerx\JsonApi\Encoder\Parser\Parser;
28
use \Neomerx\JsonApi\Parameters\Parameters;
29
use \Neomerx\JsonApi\Schema\IdentitySchema;
30
use \Neomerx\JsonApi\Schema\ResourceObject;
31
use \Neomerx\JsonApi\Encoder\EncoderOptions;
32
use \Neomerx\JsonApi\Encoder\Stack\StackFrame;
33
use \Neomerx\JsonApi\Parameters\SortParameter;
34
use \Neomerx\JsonApi\Schema\RelationshipObject;
35
use \Neomerx\JsonApi\Encoder\Parser\ParserReply;
36
use \Neomerx\JsonApi\Parameters\ParametersParser;
37
use \Neomerx\JsonApi\Encoder\Parser\ParserManager;
38
use \Neomerx\JsonApi\Parameters\Headers\MediaType;
39
use \Neomerx\JsonApi\Parameters\EncodingParameters;
40
use \Neomerx\JsonApi\Contracts\Schema\LinkInterface;
41
use \Neomerx\JsonApi\Parameters\SupportedExtensions;
42
use \Neomerx\JsonApi\Encoder\Parser\ParserEmptyReply;
43
use \Neomerx\JsonApi\Parameters\Headers\AcceptHeader;
44
use \Neomerx\JsonApi\Encoder\Handlers\ReplyInterpreter;
45
use \Neomerx\JsonApi\Parameters\RestrictiveQueryChecker;
46
use \Neomerx\JsonApi\Parameters\Headers\AcceptMediaType;
47
use \Neomerx\JsonApi\Contracts\Schema\ContainerInterface;
48
use \Neomerx\JsonApi\Contracts\Document\DocumentInterface;
49
use \Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
50
use \Neomerx\JsonApi\Parameters\RestrictiveHeadersChecker;
51
use \Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface;
52
use \Neomerx\JsonApi\Encoder\Parameters\ParametersAnalyzer;
53
use \Neomerx\JsonApi\Schema\ResourceIdentifierSchemaAdapter;
54
use \Neomerx\JsonApi\Parameters\RestrictiveParametersChecker;
55
use \Neomerx\JsonApi\Contracts\Schema\SchemaProviderInterface;
56
use \Neomerx\JsonApi\Schema\ResourceIdentifierContainerAdapter;
57
use \Neomerx\JsonApi\Contracts\Parameters\Headers\HeaderInterface;
58
use \Neomerx\JsonApi\Contracts\Encoder\Stack\StackReadOnlyInterface;
59
use \Neomerx\JsonApi\Contracts\Integration\ExceptionThrowerInterface;
60
use \Neomerx\JsonApi\Contracts\Encoder\Parser\ParserManagerInterface;
61
use \Neomerx\JsonApi\Contracts\Parameters\Headers\MediaTypeInterface;
62
use \Neomerx\JsonApi\Contracts\Parameters\EncodingParametersInterface;
63
use \Neomerx\JsonApi\Contracts\Parameters\Headers\AcceptHeaderInterface;
64
use \Neomerx\JsonApi\Contracts\Encoder\Stack\StackFrameReadOnlyInterface;
65
use \Neomerx\JsonApi\Contracts\Encoder\Parameters\ParametersAnalyzerInterface;
66
67
/**
68
 * @package Neomerx\JsonApi
69
 */
70
class Factory implements FactoryInterface
71
{
72
    /**
73
     * @inheritdoc
74
     */
75 58
    public function createEncoder(ContainerInterface $container, EncoderOptions $encoderOptions = null)
76
    {
77 58
        return new Encoder($this, $container, $encoderOptions);
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 86
    public function createDocument()
84
    {
85 86
        return new Document();
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 3
    public function createError(
92
        $idx = null,
93
        LinkInterface $aboutLink = null,
94
        $status = null,
95
        $code = null,
96
        $title = null,
97
        $detail = null,
98
        $source = null,
99
        array $meta = null
100
    ) {
101 3
        return new Error($idx, $aboutLink, $status, $code, $title, $detail, $source, $meta);
102
    }
103
    /**
104
     * @inheritdoc
105
     */
106 49
    public function createReply($replyType, StackReadOnlyInterface $stack)
107
    {
108 49
        return new ParserReply($replyType, $stack);
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114 18
    public function createEmptyReply(
115
        $replyType,
116
        StackReadOnlyInterface $stack
117
    ) {
118 18
        return new ParserEmptyReply($replyType, $stack);
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124 55
    public function createParser(ContainerInterface $container, ParserManagerInterface $manager)
125
    {
126 55
        return new Parser($this, $this, $this, $container, $manager);
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132 53
    public function createManager(ParametersAnalyzerInterface $parameterAnalyzer)
133
    {
134 53
        return new ParserManager($parameterAnalyzer);
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140 58
    public function createFrame(StackFrameReadOnlyInterface $previous = null)
141
    {
142 58
        return new StackFrame($previous);
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148 59
    public function createStack()
149
    {
150 59
        return new Stack($this);
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156 53
    public function createReplyInterpreter(DocumentInterface $document, ParametersAnalyzerInterface $parameterAnalyzer)
157
    {
158 53
        return new ReplyInterpreter($document, $parameterAnalyzer);
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164 40
    public function createEncodingParameters($includePaths = null, array $fieldSets = null)
165
    {
166 40
        return new EncodingParameters($includePaths, $fieldSets);
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172 56
    public function createParametersAnalyzer(EncodingParametersInterface $parameters, ContainerInterface $container)
173
    {
174 56
        return new ParametersAnalyzer($parameters, $container);
175
    }
176
177
    /**
178
     * @inheritdoc
179
     */
180 1
    public function createMediaType($type, $subType, $parameters = null)
181
    {
182 1
        return new MediaType($type, $subType, $parameters);
183
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188 26
    public function createParameters(
189
        HeaderInterface $contentType,
190
        AcceptHeaderInterface $accept,
191
        $includePaths = null,
192
        array $fieldSets = null,
193
        $sortParameters = null,
194
        array $pagingParameters = null,
195
        array $filteringParameters = null,
196
        array $unrecognizedParams = null
197
    ) {
198 26
        return new Parameters(
199 26
            $contentType,
200 26
            $accept,
201 26
            $includePaths,
202 26
            $fieldSets,
203 26
            $sortParameters,
204 26
            $pagingParameters,
205 26
            $filteringParameters,
206
            $unrecognizedParams
207 26
        );
208
    }
209
210
    /**
211
     * @inheritdoc
212
     */
213 34
    public function createParametersParser()
214
    {
215 34
        return new ParametersParser($this);
216
    }
217
218
    /**
219
     * @inheritdoc
220
     */
221 19
    public function createSortParam($sortField, $isAscending)
222
    {
223 19
        return new SortParameter($sortField, $isAscending);
224
    }
225
226
    /**
227
     * @inheritdoc
228
     */
229 1
    public function createSupportedExtensions($extensions = MediaTypeInterface::NO_EXT)
230
    {
231 1
        return new SupportedExtensions($extensions);
232
    }
233
234
    /**
235
     * @inheritdoc
236
     */
237 2
    public function createAcceptMediaType(
238
        $position,
239
        $type,
240
        $subType,
241
        $parameters = null,
242
        $quality = 1.0,
243
        $extensions = null
244
    ) {
245 2
        return new AcceptMediaType($position, $type, $subType, $parameters, $quality, $extensions);
246
    }
247
248
    /**
249
     * @inheritdoc
250
     */
251 2
    public function createAcceptHeader($unsortedMediaTypes)
252
    {
253 2
        return new AcceptHeader($unsortedMediaTypes);
254
    }
255
256
    /**
257
     * @inheritdoc
258
     */
259 16
    public function createHeadersChecker(
260
        ExceptionThrowerInterface $exceptionThrower,
261
        CodecMatcherInterface $codecMatcher
262
    ) {
263 16
        return new RestrictiveHeadersChecker($exceptionThrower, $codecMatcher);
264
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269 16
    public function createQueryChecker(
270
        ExceptionThrowerInterface $exceptionThrower,
271
        $allowUnrecognized = true,
272
        array $includePaths = null,
273
        array $fieldSetTypes = null,
274
        array $sortParameters = null,
275
        array $pagingParameters = null,
276
        array $filteringParameters = null
277
    ) {
278 16
        return new RestrictiveQueryChecker(
279 16
            $exceptionThrower,
280 16
            $allowUnrecognized,
281 16
            $includePaths,
282 16
            $fieldSetTypes,
283 16
            $sortParameters,
284 16
            $pagingParameters,
285
            $filteringParameters
286 16
        );
287
    }
288
289
    /**
290
     * @inheritdoc
291
     */
292 16
    public function createParametersChecker(
293
        ExceptionThrowerInterface $exceptionThrower,
294
        CodecMatcherInterface $codecMatcher,
295
        $allowUnrecognized = false,
296
        array $includePaths = null,
297
        array $fieldSetTypes = null,
298
        array $sortParameters = null,
299
        array $pagingParameters = null,
300
        array $filteringParameters = null
301
    ) {
302 16
        $headersChecker = $this->createHeadersChecker($exceptionThrower, $codecMatcher);
303 16
        $queryChecker   = $this->createQueryChecker(
304 16
            $exceptionThrower,
305 16
            $allowUnrecognized,
306 16
            $includePaths,
307 16
            $fieldSetTypes,
308 16
            $sortParameters,
309 16
            $pagingParameters,
310
            $filteringParameters
311 16
        );
312
313 16
        return new RestrictiveParametersChecker($headersChecker, $queryChecker);
314
    }
315
316
    /**
317
     * @inheritdoc
318
     */
319 70
    public function createContainer(array $providers = [])
320
    {
321 70
        return new Container($this, $providers);
322
    }
323
324
    /**
325
     * @inheritdoc
326
     */
327 70
    public function createResourceObject(
328
        SchemaProviderInterface $schema,
329
        $resource,
330
        $isInArray,
331
        $attributeKeysFilter = null
332
    ) {
333 70
        return new ResourceObject($schema, $resource, $isInArray, $attributeKeysFilter);
334
    }
335
336
    /**
337
     * @inheritdoc
338
     */
339 70
    public function createRelationshipObject($name, $data, $links, $meta, $isShowData, $isRoot)
340
    {
341 70
        return new RelationshipObject($name, $data, $links, $meta, $isShowData, $isRoot);
342
    }
343
344
    /**
345
     * @inheritdoc
346
     */
347 7
    public function createLink($subHref, $meta = null, $treatAsHref = false)
348
    {
349 7
        return new Link($subHref, $meta, $treatAsHref);
350
    }
351
352
    /**
353
     * @inheritdoc
354
     */
355 2
    public function createResourceIdentifierSchemaAdapter(SchemaProviderInterface $schema)
356
    {
357 2
        return new ResourceIdentifierSchemaAdapter($this, $schema);
358
    }
359
360
    /**
361
     * @inheritdoc
362
     */
363 2
    public function createResourceIdentifierContainerAdapter(ContainerInterface $container)
364
    {
365 2
        return new ResourceIdentifierContainerAdapter($this, $container);
366
    }
367
368
    /**
369
     * @inheritdoc
370
     */
371 1
    public function createIdentitySchema(ContainerInterface $container, $classType, Closure $identityClosure)
372
    {
373 1
        return new IdentitySchema($this, $container, $classType, $identityClosure);
374
    }
375
376
    /**
377
     * @inheritdoc
378
     */
379 28
    public function createCodecMatcher()
380
    {
381 28
        return new CodecMatcher();
382
    }
383
}
384