Passed
Push — master ( 537d79...534bb6 )
by Mariano
01:14
created

RequestConditionToArrayConverter::convertHeaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 15
rs 9.9666
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\Condition\Conditions\HeaderCondition;
22
use Mcustiel\Phiremock\Domain\Conditions;
23
use Mcustiel\Phiremock\Domain\Http\HeaderName;
24
25
class RequestConditionToArrayConverter
26
{
27
    public function convert(Conditions $request): array
28
    {
29
        $requestArray = [];
30
31
        $this->convertMethod($request, $requestArray);
32
        $this->convertUrl($request, $requestArray);
33
        $this->convertBody($request, $requestArray);
34
        $this->convertHeaders($request, $requestArray);
35
36
        return $requestArray;
37
    }
38
39
    protected function convertHeaders(Conditions $request, array &$requestArray): void
40
    {
41
        $headers = $request->getHeaders();
42
        if ($headers === null) {
43
            $requestArray['headers'] = null;
44
        } else {
45
            $headersArray = [];
46
            /** @var HeaderName $headerName */
47
            /** @var HeaderCondition $headerCondition */
48
            foreach ($headers as $headerName => $headerCondition) {
49
                $headersArray[$headerName->asString()] = [
50
                    $headerCondition->getMatcher()->getName() => $headerCondition->getValue()->asString(),
51
                ];
52
            }
53
            $requestArray['headers'] = $headersArray;
54
        }
55
    }
56
57
    protected function convertBody(Conditions $request, array &$requestArray): void
58
    {
59
        $body = $request->getBody();
60
        $requestArray['body'] = null === $body ? null : [
61
            $body->getMatcher()->getName() => $body->getValue()->asString(),
62
        ];
63
    }
64
65
    protected function convertUrl(Conditions $request, array &$requestArray): void
66
    {
67
        $url = $request->getUrl();
68
        $requestArray['url'] = null === $url ? null : [
69
            $url->getMatcher()->getName() => $url->getValue()->asString(),
70
        ];
71
    }
72
73
    protected function convertMethod(Conditions $request, array &$requestArray): void
74
    {
75
        $method = $request->getMethod();
76
        $requestArray['method'] = null === $method ? null : $method->getValue()->asString();
77
    }
78
}
79