Completed
Push — master ( 1b0d8b...4b53bc )
by John
03:07
created

RequestParameterAssembler::getRequestParameters()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 2
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions 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
namespace KleijnWeb\PhpApi\Descriptions\Request;
9
10
use KleijnWeb\PhpApi\Descriptions\Description\Operation;
11
use KleijnWeb\PhpApi\Descriptions\Description\Parameter;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class RequestParameterAssembler
18
{
19
    /**
20
     * @var ParameterCoercer
21
     */
22
    private $parameterCoercer;
23
24
    /**
25
     * ApiRequest constructor.
26
     *
27
     * @param ParameterCoercer $parameterCoercer
28
     */
29
    public function __construct(ParameterCoercer $parameterCoercer = null)
30
    {
31
        $this->parameterCoercer = $parameterCoercer ?: new ParameterCoercer();
32
    }
33
34
    /**
35
     * @param Operation $operation
36
     * @param array     $queryParams
37
     * @param array     $pathParams
38
     * @param array     $headers
39
     * @param mixed     $body
40
     *
41
     * @return \stdClass
42
     */
43
    public function assemble(
44
        Operation $operation,
45
        array $queryParams,
46
        array $pathParams,
47
        array $headers,
48
        $body = null
49
    ): \stdClass
50
    {
51
        $headerParamMap = array_combine(array_map(function ($key) {
52
            return $this->getHeaderParameterName($key);
53
        }, array_keys($headers)), array_keys($headers));
54
55
        $parameters = (object)[];
56
        foreach ($operation->getParameters() as $parameter) {
57
            $paramName = $parameter->getName();
58
59
            switch ($parameter->getIn()) {
60 View Code Duplication
                case Parameter::IN_QUERY:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
61
                    if (isset($queryParams[$paramName])) {
62
                        $parameters->$paramName = $this->parameterCoercer->coerce($parameter, $queryParams[$paramName]);
63
                    }
64
                    break;
65
                case Parameter::IN_BODY:
66
                    $parameters->$paramName = $body;
67
                    break;
68 View Code Duplication
                case Parameter::IN_PATH:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
                    if (isset($pathParams[$paramName])) {
70
                        $parameters->$paramName = $this->parameterCoercer->coerce($parameter, $pathParams[$paramName]);
71
                    }
72
                    break;
73
                case Parameter::IN_HEADER:
74
                    if (isset($headers[$paramName])) {
75
                        $value = $headers[$paramName];
76
                    } elseif (isset($headerParamMap[$paramName])) {
77
                        $value = $headers[$headerParamMap[$paramName]];
78
                    } else {
79
                        break;
80
                    }
81
                    $parameters->$paramName = $this->parameterCoercer->coerce($parameter, $value);
82
                    break;
83
            }
84
        }
85
86
        return $parameters;
87
    }
88
89
    /**
90
     * @param ServerRequestInterface $httpRequest
91
     * @param Operation              $operation
92
     *
93
     * @return \stdClass
94
     */
95
    public function getRequestParameters(ServerRequestInterface $httpRequest, Operation $operation): \stdClass
96
    {
97
        $indexed = array_combine(
98
            explode('/', trim($operation->getPath(), '/')),
99
            explode('/', trim($httpRequest->getUri()->getPath(), '/'))
100
        );
101
102
        $pathParams = [];
103
104
        foreach ($indexed as $key => $value) {
105
            if (0 == strpos('{', $key)) {
106
                $pathParams[trim($key, '{}')] = $value;
107
            }
108
        }
109
110
        $queryParams = $httpRequest->getQueryParams();
111
        $headers     = $httpRequest->getHeaders();
112
113
        return $this->assemble(
114
            $operation,
115
            $queryParams,
116
            $pathParams,
117
            $headers,
118
            $httpRequest->getParsedBody()
119
        );
120
    }
121
122
    /**
123
     * @param string $headerName
124
     *
125
     * @return string
126
     */
127
    private function getHeaderParameterName(string $headerName)
128
    {
129
        $replacements = [
130
            function ($matches) {
131
                return strtolower($matches[2]);
132
            },
133
            function ($matches) {
134
                return strtoupper($matches[2]);
135
            },
136
            function ($matches) {
137
                return strtoupper($matches[1]);
138
            },
139
        ];
140
141
        foreach (['/^(X-)?(.*)/i', '/(\-)([\S]{1})/', '/(^[\S]{1})/',] as $index => $pattern) {
142
            $headerName = preg_replace_callback($pattern, $replacements[$index], $headerName);
143
        }
144
145
        return lcfirst($headerName);
146
    }
147
}
148