ResponseTranslator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 52
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A translate() 0 19 2
A translateHeaders() 0 19 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Translator;
5
6
use Innmind\Http\{
7
    Message\Response,
8
    Headers,
9
    Header\Value
10
};
11
use Innmind\Immutable\Map;
12
use Symfony\Component\HttpFoundation\Response as SfResponse;
13
14
final class ResponseTranslator
15
{
16
    private $transformed;
17
18 8
    public function __construct()
19
    {
20 8
        $this->transformed = new Map(
21 8
            Response::class,
22 8
            SfResponse::class
23
        );
24 8
    }
25
26 4
    public function translate(Response $response): SfResponse
27
    {
28 4
        if ($this->transformed->contains($response)) {
29 2
            return $this->transformed->get($response);
30
        }
31
32 4
        $sfResponse = (new SfResponse(
33 4
            (string) $response->body(),
34 4
            $response->statusCode()->value(),
35 4
            $this->translateHeaders($response->headers())
36
        ))
37 4
            ->setProtocolVersion((string) $response->protocolVersion());
38 4
        $this->transformed = $this->transformed->put(
39 4
            $response,
40 4
            $sfResponse
41
        );
42
43 4
        return $sfResponse;
44
    }
45
46 4
    private function translateHeaders(Headers $headers): array
47
    {
48 4
        $raw = [];
49
50 4
        foreach ($headers as $header) {
51 4
            $raw[$header->name()] = $header
52 4
                ->values()
53 4
                ->reduce(
54 4
                    [],
55 4
                    function(array $carry, Value $value) {
56 4
                        $carry[] = (string) $value;
57
58 4
                        return $carry;
59 4
                    }
60
                );
61
        }
62
63
        return $raw;
64
    }
65
}
66