HttpResponseToArrayConverter::convert()   A
last analyzed

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 15
    public function convert(Response $response): array
28
    {
29
        /** @var HttpResponse $response */
30 15
        $responseArray = [];
31 15
        $responseArray['statusCode'] = $response->getStatusCode()->asInt();
32 15
        $body = $response->getBody();
33 15
        $responseArray['body'] = $body === null ? null : $body->asString();
34 15
        $headers = $response->getHeaders();
35 15
        if ($headers && !$headers->isEmpty()) {
36 3
            $responseArray['headers'] = $this->convertHeaders($response);
37
        } else {
38 12
            $responseArray['headers'] = null;
39
        }
40
41 15
        return array_merge(parent::convert($response), ['response' => $responseArray]);
42
    }
43
44 3
    private function convertHeaders(HttpResponse $response): array
45
    {
46 3
        $headers = $response->getHeaders();
47 3
        $headersArray = [];
48
        /** @var Header $header */
49 3
        foreach ($headers as $header) {
50 3
            $headersArray[$header->getName()->asString()] = $header->getValue()->asString();
51
        }
52
53 3
        return $headersArray;
54
    }
55
}
56