Completed
Push — master ( 48acde...f7424d )
by Alejandro
07:34
created

BodyParserMiddleware   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 93
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 4
A getRequestContentType() 0 6 1
A parseFromJson() 0 14 3
A parseFromUrlEncoded() 0 12 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
     */
36 3
    public function __invoke(Request $request, Response $response, callable $out = null)
37
    {
38 3
        $method = $request->getMethod();
39 3
        $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 3
        if (in_array($method, ['GET', 'HEAD', 'OPTIONS']) || ! empty($currentParams)) {
43 1
            return $out($request, $response);
44
        }
45
46
        // If the accepted content is JSON, try to parse the body from JSON
47 3
        $contentType = $this->getRequestContentType($request);
48 3
        if (in_array($contentType, ['application/json', 'text/json', 'application/x-json'])) {
49 1
            return $out($this->parseFromJson($request), $response);
50
        }
51
52 2
        return $out($this->parseFromUrlEncoded($request), $response);
53
    }
54
55
    /**
56
     * @param Request $request
57
     * @return string
58
     */
59 3
    protected function getRequestContentType(Request $request)
60
    {
61 3
        $contentType = $request->getHeaderLine('Content-type');
62 3
        $contentTypes = explode(';', $contentType);
63 3
        return trim(array_shift($contentTypes));
64
    }
65
66
    /**
67
     * @param Request $request
68
     * @return Request
69
     */
70 1
    protected function parseFromJson(Request $request)
71
    {
72 1
        $rawBody = (string) $request->getBody();
73 1
        if (empty($rawBody)) {
74
            return $request;
75
        }
76
77 1
        $parsedJson = json_decode($rawBody, true);
78 1
        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 1
        return $request->withParsedBody($parsedJson);
83
    }
84
85
    /**
86
     * @param Request $request
87
     * @return Request
88
     */
89 2
    protected function parseFromUrlEncoded(Request $request)
90
    {
91 2
        $rawBody = (string) $request->getBody();
92 2
        if (empty($rawBody)) {
93 1
            return $request;
94
        }
95
96 1
        $parsedBody = [];
97 1
        parse_str($rawBody, $parsedBody);
98
99 1
        return $request->withParsedBody($parsedBody);
100
    }
101
}
102