Completed
Pull Request — master (#49)
by John
02:41
created

RequestCoercer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 6
c 7
b 0
f 0
lcom 1
cbo 5
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B coerceRequest() 0 32 5
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Request;
10
11
use KleijnWeb\SwaggerBundle\Document\OperationObject;
12
use KleijnWeb\SwaggerBundle\Exception\UnsupportedException;
13
use Symfony\Component\HttpFoundation\Request;
14
use KleijnWeb\SwaggerBundle\Exception\MalformedContentException;
15
16
/**
17
 * @author John Kleijn <[email protected]>
18
 */
19
class RequestCoercer
20
{
21
    /**
22
     * @var ContentDecoder
23
     */
24
    private $contentDecoder;
25
26
    /**
27
     * @param ContentDecoder $contentDecoder
28
     */
29
    public function __construct(ContentDecoder $contentDecoder)
30
    {
31
        $this->contentDecoder = $contentDecoder;
32
    }
33
34
    /**
35
     * @param Request         $request
36
     * @param OperationObject $operationObject
37
     *
38
     * @throws MalformedContentException
39
     * @throws UnsupportedException
40
     */
41
    public function coerceRequest(Request $request, OperationObject $operationObject)
42
    {
43
        $content = $this->contentDecoder->decodeContent($request, $operationObject);
44
45
        $paramBagMapping = [
46
            'query'  => 'query',
47
            'path'   => 'attributes',
48
            'header' => 'headers'
49
        ];
50
        foreach ($operationObject->getDefinition()->parameters as $paramDefinition) {
51
            $paramName = $paramDefinition->name;
52
53
            if ($paramDefinition->in === 'body') {
54
                if ($content !== null) {
55
                    $request->attributes->set($paramName, $content);
56
                }
57
58
                continue;
59
            }
60
            $paramBagName = $paramBagMapping[$paramDefinition->in];
61
            if (!$request->$paramBagName->has($paramName)) {
62
                continue;
63
            }
64
            $request->attributes->set(
65
                $paramName,
66
                ParameterCoercer::coerceParameter(
67
                    $paramDefinition,
68
                    $request->$paramBagName->get($paramName)
69
                )
70
            );
71
        }
72
    }
73
}
74