Input::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Equip;
4
5
use Equip\Adr\InputInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
class Input implements InputInterface
9
{
10
    /**
11
     * Flatten all input from the request.
12
     *
13
     * @param ServerRequestInterface $request
14
     *
15
     * @return array
16
     */
17 10
    public function __invoke(
18
        ServerRequestInterface $request
19
    ) {
20 10
        $attrs = $request->getAttributes();
21 10
        $body = $this->body($request);
22 10
        $cookies = $request->getCookieParams();
23 10
        $query = $request->getQueryParams();
24 10
        $uploads = $request->getUploadedFiles();
25
26
        // Order matters here! Important values go last!
27 10
        return array_replace(
28 10
            $query,
29 10
            $body,
30 10
            $uploads,
31 10
            $cookies,
32
            $attrs
33 10
        );
34
    }
35
36
    /**
37
     * @param ServerRequestInterface $request
38
     *
39
     * @return array
40
     */
41 10
    private function body(ServerRequestInterface $request)
42
    {
43 10
        $body = $request->getParsedBody();
44
45 10
        if (empty($body)) {
46 8
            return [];
47
        }
48
49 3
        if (is_object($body)) {
50
            // Because the parsed body may also be represented as an object,
51
            // additional parsing is required. This is a bit dirty but works
52
            // very well for anonymous objects.
53 1
            $body = json_decode(json_encode($body), true);
54 1
        }
55
56 3
        return $body;
57
    }
58
}
59