Passed
Push — master ( a33cf4...6335da )
by Mariano
01:54
created

HttpResponseToArrayConverter::convert()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Phiremock.
4
 *
5
 * Phiremock is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * Phiremock is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with Phiremock.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Mcustiel\Phiremock\Common\Utils\V2;
20
21
use Mcustiel\Phiremock\Domain\Http\Header;
22
use Mcustiel\Phiremock\Domain\HttpResponse;
23
use Mcustiel\Phiremock\Domain\Response;
24
25
class HttpResponseToArrayConverter extends ResponseToArrayConverter
26
{
27 3
    public function convert(Response $response): array
28
    {
29
        /** @var HttpResponse $response */
30 3
        $responseArray = [];
31 3
        $responseArray['statusCode'] = $response->getStatusCode()->asInt();
32 3
        $body = $response->getBody();
33 3
        $responseArray['body'] = $body === null ? null : $body->asString();
34 3
        $headers = $response->getHeaders();
35 3
        if ($headers && !$headers->isEmpty()) {
36 1
            $responseArray['headers'] = $this->convertHeaders($response);
37
        } else {
38 2
            $responseArray['headers'] = null;
39
        }
40
41 3
        return array_merge(parent::convert($response), ['response' => $responseArray]);
42
    }
43
44 1
    private function convertHeaders(HttpResponse $response): array
45
    {
46 1
        $headers = $response->getHeaders();
47 1
        $headersArray = [];
48
        /** @var Header $header */
49 1
        foreach ($headers as $header) {
50 1
            $headersArray[$header->getName()->asString()] = $header->getValue()->asString();
51
        }
52
53 1
        return $headersArray;
54
    }
55
}
56