Passed
Push — master ( d14e7d...cb0a20 )
by Mariano
02:08
created

ArrayToHttpResponseConverter::convertResponse()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.026

Importance

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