Failed Conditions
Pull Request — master (#47)
by Florent
12:38
created

JsonBodyParserMiddleware::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Server\Middleware;
15
16
use Interop\Http\ServerMiddleware\DelegateInterface;
17
use Interop\Http\ServerMiddleware\MiddlewareInterface;
18
use OAuth2Framework\Component\Server\Response\OAuth2Exception;
19
use OAuth2Framework\Component\Server\Response\OAuth2ResponseFactoryManager;
20
use Psr\Http\Message\ServerRequestInterface;
21
22
final class JsonBodyParserMiddleware implements MiddlewareInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
28
    {
29
        $headers = $request->getHeader('content-type');
30
        foreach ($headers as $header) {
31
            if ('application/json' === substr($header, 0, 16)) {
32
                $request->getBody()->rewind();
33
                $body = $request->getBody()->getContents();
34
                $json = json_decode($body, true);
35
                if (is_array($json)) {
36
                    $request = $request->withParsedBody($json);
37
                }
38
            }
39
        }
40
41
        return $delegate->process($request);
42
    }
43
}
44