Formatter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 59
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A formatRequest() 0 13 1
A formatResponse() 0 11 1
A formatException() 0 9 1
A filterParameters() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Event\Formatter;
13
14
use Ivory\HttpAdapter\HttpAdapterException;
15
use Ivory\HttpAdapter\Message\InternalRequestInterface;
16
use Ivory\HttpAdapter\Message\ResponseInterface;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class Formatter implements FormatterInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 9
    public function formatRequest(InternalRequestInterface $request)
27
    {
28
        return [
29 9
            'protocol_version' => $request->getProtocolVersion(),
30 9
            'uri'              => (string) $request->getUri(),
31 9
            'method'           => $request->getMethod(),
32 9
            'headers'          => $request->getHeaders(),
33 9
            'body'             => utf8_encode((string) $request->getBody()),
34 9
            'datas'            => $request->getDatas(),
35 9
            'files'            => $request->getFiles(),
36 9
            'parameters'       => $this->filterParameters($request->getParameters()),
37 7
        ];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 9
    public function formatResponse(ResponseInterface $response)
44
    {
45
        return [
46 9
            'protocol_version' => $response->getProtocolVersion(),
47 9
            'status_code'      => $response->getStatusCode(),
48 9
            'reason_phrase'    => $response->getReasonPhrase(),
49 9
            'headers'          => $response->getHeaders(),
50 9
            'body'             => utf8_encode((string) $response->getBody()),
51 9
            'parameters'       => $this->filterParameters($response->getParameters()),
52 7
        ];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 9
    public function formatException(HttpAdapterException $exception)
59
    {
60
        return [
61 9
            'code'    => $exception->getCode(),
62 9
            'message' => $exception->getMessage(),
63 9
            'line'    => $exception->getLine(),
64 9
            'file'    => $exception->getFile(),
65 7
        ];
66
    }
67
68
    /**
69
     * @param array $parameters
70
     *
71
     * @return array
72
     */
73
    private function filterParameters(array $parameters)
74
    {
75 18
        return array_filter($parameters, function ($parameter) {
76 18
            return !is_object($parameter) && !is_resource($parameter);
77 18
        });
78
    }
79
}
80