Test Failed
Push — master ( 40a540...b96576 )
by Mariano
01:45
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\Common\Utils\ArrayToHttpResponseConverter as ArrayToHttpResponseConverterInterface;
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 implements ArrayToHttpResponseConverterInterface
36
{
37
    const ALLOWED_OPTIONS = [
38
        'statusCode' => null,
39
        'body'       => null,
40
        'headers'    => null,
41
        'delayMillis'=> null,
42
    ];
43
    const STRING_START = 0;
44
45
    public function convert(array $responseArray): HttpResponse
46
    {
47
        return parent::convert($responseArray);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::convert($responseArray) returns the type Mcustiel\Phiremock\Domain\Response which includes types incompatible with the type-hinted return Mcustiel\Phiremock\Domain\HttpResponse.
Loading history...
48
    }
49
50
    protected function convertResponse(
51
        array $responseArray,
52
        ?Delay $delay,
53
        ?ScenarioState $newScenarioState
54
    ): Response {
55
        if (!isset($responseArray['response']['statusCode'])) {
56
            $responseArray['response']['statusCode'] = 200;
57
        }
58
59
        return new HttpResponse(
60
            new StatusCode((int) $responseArray['response']['statusCode']),
61
            $this->getBody($responseArray['response']),
62
            $this->getHeaders($responseArray['response']),
63
            $delay,
64
            $newScenarioState
65
        );
66
    }
67
68
    private function getHeaders(array $responseArray): ?HeadersCollection
69
    {
70
        if (isset($responseArray['headers'])) {
71
            $headers = $responseArray['headers'];
72
            if (!empty($headers)) {
73
                if (!\is_array($headers)) {
74
                    throw new \InvalidArgumentException('Response headers are invalid: ' . var_export($headers, true));
75
                }
76
77
                return $this->convertHeaders($headers);
78
            }
79
        }
80
81
        return null;
82
    }
83
84
    private function getBody(array $responseArray): ?Body
85
    {
86
        if (isset($responseArray['body'])) {
87
            $body = $responseArray['body'];
88
            if (\is_array($body)) {
89
                $body = json_encode($body);
90
            }
91
            if ($this->isBinaryBody($body)) {
92
                return new BinaryBody($body);
93
            }
94
95
            return new Body($body);
96
        }
97
98
        return null;
99
    }
100
101
    private function isBinaryBody(string $body): bool
102
    {
103
        return BinaryInfo::BINARY_BODY_PREFIX === substr($body, self::STRING_START, BinaryInfo::BINARY_BODY_PREFIX_LENGTH);
104
    }
105
106
    /**
107
     * @param array $headers
108
     *
109
     * @throws \InvalidArgumentException
110
     *
111
     * @return \Mcustiel\Phiremock\Domain\Http\HeadersCollection
112
     */
113
    private function convertHeaders(array $headers): HeadersCollection
114
    {
115
        $headerCollection = new HeadersCollection();
116
        foreach ($headers as $headerName => $headerValue) {
117
            $headerCollection->setHeader(
118
                new Header(
119
                    new HeaderName($headerName),
120
                    new HeaderValue($headerValue)
121
                )
122
            );
123
        }
124
125
        return $headerCollection;
126
    }
127
}
128