Completed
Pull Request — master (#44)
by Tobias
03:28
created

FullHttpMessageFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Http\Message\Formatter;
4
5
use Http\Message\Formatter;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * A formatter that prints the complete HTTP message.
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class FullHttpMessageFormatter implements Formatter
15
{
16
    /**
17
     * The maximum length of the body.
18
     *
19
     * @var int
20
     */
21
    private $maxBodyLendth;
22
23
    /**
24
     * @param int $maxBodyLendth number of chars.
25
     */
26
    public function __construct($maxBodyLendth = 1000)
27
    {
28
        $this->maxBodyLendth = $maxBodyLendth;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 View Code Duplication
    public function formatRequest(RequestInterface $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $message = sprintf(
37
            "%s %s HTTP/%s\n",
38
            $request->getMethod(),
39
            $request->getRequestTarget(),
40
            $request->getProtocolVersion()
41
        );
42
43
        foreach ($request->getHeaders() as $name => $values) {
44
            $message .= $name.': '.implode(', ', $values)."\n";
45
        }
46
47
        $message .= "\n".mb_substr($request->getBody()->__toString(), 0, $this->maxBodyLendth);
48
49
        return $message;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 View Code Duplication
    public function formatResponse(ResponseInterface $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $message = sprintf(
58
            "HTTP/%s %s %s\n",
59
            $response->getProtocolVersion(),
60
            $response->getStatusCode(),
61
            $response->getReasonPhrase()
62
        );
63
64
        foreach ($response->getHeaders() as $name => $values) {
65
            $message .= $name.': '.implode(', ', $values)."\n";
66
        }
67
68
        $message .= "\n".mb_substr($response->getBody()->__toString(), 0, $this->maxBodyLendth);
69
70
        return $message;
71
    }
72
}
73