Passed
Push — master ( 838399...c8a626 )
by Mariano
07:20
created

ArrayToHttpResponseConverter::isBinaryBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\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 5
    protected function convertResponse(
45
        array $responseArray,
46
        ?Delay $delay,
47
        ?ScenarioState $newScenarioState
48
    ): Response {
49 5
        $response = $responseArray['response'];
50
51 5
        if (!isset($responseArray['response'])) {
52
            $responseArray['response'] = [];
53
        }
54
55 5
        if (!\is_array($responseArray['response'])) {
56
            throw new \Exception('Invalid response definition: ' . var_export($responseArray['response'], true));
57
        }
58
59 5
        $this->ensureNotInvalidOptionsAreProvided(
60 5
            $responseArray['response'],
61 5
            self::ALLOWED_OPTIONS
62
        );
63 5
        if (!isset($response['statusCode'])) {
64 2
            $response['statusCode'] = 200;
65
        }
66
67 5
        return new HttpResponse(
68 5
            new StatusCode((int) $response['statusCode']),
69 5
            $this->getBody($response),
70 5
            $this->getHeaders($response),
71 5
            $delay,
72 5
            $newScenarioState
73
        );
74
    }
75
76 5
    private function getHeaders(array $responseArray): ?HeadersCollection
77
    {
78 5
        if (isset($responseArray['headers'])) {
79 1
            $headers = $responseArray['headers'];
80 1
            if (!empty($headers)) {
81 1
                if (!\is_array($headers)) {
82
                    throw new \InvalidArgumentException('Response headers are invalid: ' . var_export($headers, true));
83
                }
84
85 1
                return $this->convertHeaders($headers);
86
            }
87
        }
88
89 4
        return null;
90
    }
91
92 5
    private function getBody(array $responseArray): ?Body
93
    {
94 5
        if (isset($responseArray['body'])) {
95 1
            $body = $responseArray['body'];
96 1
            if (\is_array($body)) {
97
                $body = json_encode($body);
98
            }
99 1
            if ($this->isBinaryBody($body)) {
100
                return new BinaryBody($body);
101
            }
102
103 1
            return new Body($body);
104
        }
105
106 4
        return null;
107
    }
108
109 1
    private function isBinaryBody(string $body): bool
110
    {
111 1
        return BinaryInfo::BINARY_BODY_PREFIX === substr($body, self::STRING_START, BinaryInfo::BINARY_BODY_PREFIX_LENGTH);
112
    }
113
114 1
    private function convertHeaders(array $headers): HeadersCollection
115
    {
116 1
        $headerCollection = new HeadersCollection();
117 1
        foreach ($headers as $headerName => $headerValue) {
118 1
            $headerCollection->setHeader(
119 1
                new Header(
120 1
                    new HeaderName($headerName),
121 1
                    new HeaderValue($headerValue)
122
                )
123
            );
124
        }
125
126 1
        return $headerCollection;
127
    }
128
}
129