Test Failed
Push — master ( b96576...4c3f74 )
by Mariano
01:50
created

ArrayToHttpResponseConverter::convert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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