Passed
Push — master ( d6afed...986f08 )
by Mariano
01:15
created

HttpResponseToArrayConverter::convertHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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