Completed
Push — master ( 6eef69...27b08f )
by Alejandro
07:50
created

BodyParserMiddleware::getRequestContentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
namespace Shlinkio\Shlink\Rest\Middleware;
3
4
use Psr\Http\Message\ResponseInterface as Response;
5
use Psr\Http\Message\ServerRequestInterface as Request;
6
use Shlinkio\Shlink\Common\Exception\RuntimeException;
7
use Zend\Stratigility\MiddlewareInterface;
8
9
class BodyParserMiddleware implements MiddlewareInterface
10
{
11
    /**
12
     * Process an incoming request and/or response.
13
     *
14
     * Accepts a server-side request and a response instance, and does
15
     * something with them.
16
     *
17
     * If the response is not complete and/or further processing would not
18
     * interfere with the work done in the middleware, or if the middleware
19
     * wants to delegate to another process, it can use the `$out` callable
20
     * if present.
21
     *
22
     * If the middleware does not return a value, execution of the current
23
     * request is considered complete, and the response instance provided will
24
     * be considered the response to return.
25
     *
26
     * Alternately, the middleware may return a response instance.
27
     *
28
     * Often, middleware will `return $out();`, with the assumption that a
29
     * later middleware will return a response.
30
     *
31
     * @param Request $request
32
     * @param Response $response
33
     * @param null|callable $out
34
     * @return null|Response
35 3
     */
36
    public function __invoke(Request $request, Response $response, callable $out = null)
37 3
    {
38 3
        $method = $request->getMethod();
39 1
        $currentParams = $request->getParsedBody();
40
41
        // In requests that do not allow body or if the body has already been parsed, continue to next middleware
42 2
        if (in_array($method, ['GET', 'HEAD', 'OPTIONS']) || ! empty($currentParams)) {
43 2
            return $out($request, $response);
44 2
        }
45 1
46
        // If the accepted content is JSON, try to parse the body from JSON
47
        $contentType = $this->getRequestContentType($request);
48 1
        if (in_array($contentType, ['application/json', 'text/json', 'application/x-json'])) {
49 1
            return $out($this->parseFromJson($request), $response);
50 1
        }
51
52
        return $out($this->parseFromUrlEncoded($request), $response);
53
    }
54
55
    /**
56
     * @param Request $request
57
     * @return string
58
     */
59
    protected function getRequestContentType(Request $request)
60
    {
61
        $contentType = $request->getHeaderLine('Content-type');
62
        $contentTypes = explode(';', $contentType);
63
        return trim(array_shift($contentTypes));
64
    }
65
66
    /**
67
     * @param Request $request
68
     * @return Request
69
     */
70
    protected function parseFromJson(Request $request)
71
    {
72
        $rawBody = (string) $request->getBody();
73
        if (empty($rawBody)) {
74
            return $request;
75
        }
76
77
        $parsedJson = json_decode($rawBody, true);
78
        if (json_last_error() !== JSON_ERROR_NONE) {
79
            throw new RuntimeException(sprintf('Error when parsing JSON request body: %s', json_last_error_msg()));
80
        }
81
82
        return $request->withParsedBody($parsedJson);
83
    }
84
85
    /**
86
     * @param Request $request
87
     * @return Request
88
     */
89
    protected function parseFromUrlEncoded(Request $request)
90
    {
91
        $rawBody = (string) $request->getBody();
92
        if (empty($rawBody)) {
93
            return $request;
94
        }
95
96
        $parsedBody = [];
97
        parse_str($rawBody, $parsedBody);
98
99
        return $request->withParsedBody($parsedBody);
100
    }
101
}
102