Completed
Push — master ( 08b488...ad6d35 )
by maxime
02:09
created

FormContentMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 23
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 9 2
A parse() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Http\Middleware;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\StreamInterface;
12
13
class FormContentMiddleware implements MiddlewareInterface
14
{
15
    protected $type = 'application/x-www-form-urlencoded';
16
17 3
    public function process(ServerRequestInterface $req, RequestHandlerInterface $handler): ResponseInterface
18
    {
19 3
        if (0 === stripos($req->getHeaderLine('Content-Type'), $this->type))
20
        {
21 2
            $req = $req->withParsedBody($this->parse($req->getBody()));
22
        }
23
24 3
        return $handler->handle($req);
25
    }
26
27 2
    protected function parse(StreamInterface $body): array
28
    {
29 2
        $data = [];
30
31 2
        parse_str($body->__toString(), $data);
32
33 2
        return $data;
34
    }
35
}