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

JsonBodyParserMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 16 4
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