Test Failed
Push — master ( a9ed36...6a08f6 )
by Felipe
05:50
created

EchoBodyMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 39
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 14 3
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of coisa/http.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\Http\Middleware;
15
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
20
21
/**
22
 * Class EchoBodyMiddleware
23
 *
24
 * @package CoiSA\Http\Middleware
25
 */
26
final class EchoBodyMiddleware implements MiddlewareInterface
27
{
28
    /**
29
     * @const string
30
     */
31 2
    const DEFAULT_BUFFER_SIZE = 1024 * 8;
32
33 2
    /**
34 2
     * @var int
35
     */
36 2
    private $bufferSize;
37
38
    /**
39
     * EchoBodyMiddleware constructor.
40
     *
41
     * @param int $bufferSize
42
     */
43
    public function __construct(int $bufferSize = self::DEFAULT_BUFFER_SIZE)
44
    {
45
        $this->bufferSize = $bufferSize;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
52
    {
53
        $response = $handler->handle($request);
54
        $stream   = $response->getBody();
55
56
        if ($stream->isSeekable()) {
57
            $stream->rewind();
58
        }
59
60
        while (!$stream->eof()) {
61
            echo $stream->read($this->bufferSize);
62
        }
63
64
        return $response;
65
    }
66
}
67