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

ArrayToHttpResponseConverter::convertResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
nc 2
nop 3
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 2
rs 9.8666
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\V2;
20
21
use Mcustiel\Phiremock\Common\Utils\V1\ArrayToHttpResponseConverter as ArrayToHttpResponseConverterV1;
22
use Mcustiel\Phiremock\Domain\BinaryInfo;
23
use Mcustiel\Phiremock\Domain\Http\BinaryBody;
24
use Mcustiel\Phiremock\Domain\Http\Body;
25
use Mcustiel\Phiremock\Domain\Http\Header;
26
use Mcustiel\Phiremock\Domain\Http\HeaderName;
27
use Mcustiel\Phiremock\Domain\Http\HeadersCollection;
28
use Mcustiel\Phiremock\Domain\Http\HeaderValue;
29
use Mcustiel\Phiremock\Domain\Http\StatusCode;
30
use Mcustiel\Phiremock\Domain\HttpResponse;
31
use Mcustiel\Phiremock\Domain\Options\Delay;
32
use Mcustiel\Phiremock\Domain\Options\ScenarioState;
33
use Mcustiel\Phiremock\Domain\Response;
34
35
class ArrayToHttpResponseConverter extends ArrayToResponseConverter // extends ArrayToHttpResponseConverterV1
36
{
37
    const ALLOWED_OPTIONS = [
38
        'statusCode' => null,
39
        'body'       => null,
40
        'headers'    => null,
41
    ];
42
    const STRING_START = 0;
43
44 3
    protected function convertResponse(
45
        array $responseArray,
46
        ?Delay $delay,
47
        ?ScenarioState $newScenarioState
48
    ): Response {
49 3
        $response = $responseArray['response'];
50
51 3
        $this->ensureNotInvalidOptionsAreProvided(
52 3
            $responseArray['response'],
53 3
            self::ALLOWED_OPTIONS
54
        );
55 3
        if (!isset($response['statusCode'])) {
56 1
            $response['statusCode'] = 200;
57
        }
58
59 3
        return new HttpResponse(
60 3
            new StatusCode((int) $response['statusCode']),
61 3
            $this->getBody($response),
62 3
            $this->getHeaders($response),
63 3
            $delay,
64 3
            $newScenarioState
65
        );
66
    }
67
68 3
    private function getHeaders(array $responseArray): ?HeadersCollection
69
    {
70 3
        if (isset($responseArray['headers'])) {
71 1
            $headers = $responseArray['headers'];
72 1
            if (!empty($headers)) {
73 1
                if (!\is_array($headers)) {
74
                    throw new \InvalidArgumentException('Response headers are invalid: ' . var_export($headers, true));
75
                }
76
77 1
                return $this->convertHeaders($headers);
78
            }
79
        }
80
81 2
        return null;
82
    }
83
84 3
    private function getBody(array $responseArray): ?Body
85
    {
86 3
        if (isset($responseArray['body'])) {
87 1
            $body = $responseArray['body'];
88 1
            if (\is_array($body)) {
89
                $body = json_encode($body);
90
            }
91 1
            if ($this->isBinaryBody($body)) {
92
                return new BinaryBody($body);
93
            }
94
95 1
            return new Body($body);
96
        }
97
98 2
        return null;
99
    }
100
101 1
    private function isBinaryBody(string $body): bool
102
    {
103 1
        return BinaryInfo::BINARY_BODY_PREFIX === substr($body, self::STRING_START, BinaryInfo::BINARY_BODY_PREFIX_LENGTH);
104
    }
105
106 1
    private function convertHeaders(array $headers): HeadersCollection
107
    {
108 1
        $headerCollection = new HeadersCollection();
109 1
        foreach ($headers as $headerName => $headerValue) {
110 1
            $headerCollection->setHeader(
111 1
                new Header(
112 1
                    new HeaderName($headerName),
113 1
                    new HeaderValue($headerValue)
114
                )
115
            );
116
        }
117
118 1
        return $headerCollection;
119
    }
120
}
121