ResponseConvertor::convert()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Jasny\Codeception;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Symfony\Component\BrowserKit\Response as BrowserKitResponse;
7
8
/**
9
 * Convert a PSR-7 response to a codeception response
10
 */
11
class ResponseConvertor
12
{
13
    /**
14
     * Convert a PSR-7 response to a codeception response
15
     *
16
     * @param ResponseInterface $psrResponse
17
     * @return BrowserKitResponse
18
     */
19 1
    public function convert(ResponseInterface $psrResponse)
20
    {
21 1
        return new BrowserKitResponse(
22 1
            (string)$psrResponse->getBody(),
23 1
            $psrResponse->getStatusCode() ?: 200,
24 1
            $this->flattenHeaders($psrResponse->getHeaders())
25
        );
26
    }
27
    
28
    /**
29
     * Flatten headers
30
     *
31
     * @param array $headers
32
     * @return array
33
     */
34 1
    protected function flattenHeaders(array $headers)
35
    {
36 1
        foreach ($headers as &$value) {
37 1
            $value = is_array($value) ? end($value) : $value;
38
        }
39
        
40 1
        return $headers;
41
    }
42
}
43