RequestConditionToArrayConverter   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 71
ccs 41
cts 41
cp 1
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 11 1
A convertFormData() 0 15 3
A convertMethod() 0 4 2
A convertBody() 0 5 2
A convertHeaders() 0 15 3
A convertUrl() 0 5 2
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\RequestConditionToArrayConverter as RequestConditionToArrayConverterInterface;
22
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormFieldCondition;
23
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderCondition;
24
use Mcustiel\Phiremock\Domain\Conditions;
25
use Mcustiel\Phiremock\Domain\Http\FormFieldName;
26
use Mcustiel\Phiremock\Domain\Http\HeaderName;
27
28
class RequestConditionToArrayConverter implements RequestConditionToArrayConverterInterface
29
{
30 9
    public function convert(Conditions $request): array
31
    {
32 9
        $requestArray = [];
33
34 9
        $this->convertMethod($request, $requestArray);
35 9
        $this->convertUrl($request, $requestArray);
36 9
        $this->convertBody($request, $requestArray);
37 9
        $this->convertHeaders($request, $requestArray);
38 9
        $this->convertFormData($request, $requestArray);
39
40 9
        return $requestArray;
41
    }
42
43 24
    protected function convertHeaders(Conditions $request, array &$requestArray): void
44
    {
45 24
        $headers = $request->getHeaders();
46 24
        if ($headers === null) {
47 18
            $requestArray['headers'] = null;
48
        } else {
49 6
            $headersArray = [];
50
            /** @var HeaderName $headerName */
51
            /** @var HeaderCondition $headerCondition */
52 6
            foreach ($headers as $headerName => $headerCondition) {
53 6
                $headersArray[$headerName->asString()] = [
54 6
                    $headerCondition->getMatcher()->getName() => $headerCondition->getValue()->asString(),
55
                ];
56
            }
57 6
            $requestArray['headers'] = $headersArray;
58
        }
59 24
    }
60
61 24
    protected function convertFormData(Conditions $request, array &$requestArray): void
62
    {
63 24
        $formFields = $request->getFormFields();
64 24
        if ($formFields === null) {
65 18
            $requestArray['formData'] = null;
66
        } else {
67 6
            $fieldsArray = [];
68
            /** @var FormFieldName $fieldName */
69
            /** @var FormFieldCondition $fieldCondition */
70 6
            foreach ($formFields as $fieldName => $fieldCondition) {
71 6
                $fieldsArray[$fieldName->asString()] = [
72 6
                    $fieldCondition->getMatcher()->getName() => $fieldCondition->getValue()->asString(),
73
                ];
74
            }
75 6
            $requestArray['formData'] = $fieldsArray;
76
        }
77 24
    }
78
79 24
    protected function convertBody(Conditions $request, array &$requestArray): void
80
    {
81 24
        $body = $request->getBody();
82 24
        $requestArray['body'] = null === $body ? null : [
83 12
            $body->getMatcher()->getName() => $body->getValue()->asString(),
84
        ];
85 24
    }
86
87 24
    protected function convertUrl(Conditions $request, array &$requestArray): void
88
    {
89 24
        $url = $request->getUrl();
90 24
        $requestArray['url'] = null === $url ? null : [
91 6
            $url->getMatcher()->getName() => $url->getValue()->asString(),
92
        ];
93 24
    }
94
95 9
    protected function convertMethod(Conditions $request, array &$requestArray): void
96
    {
97 9
        $method = $request->getMethod();
98 9
        $requestArray['method'] = null === $method ? null : $method->getValue()->asString();
99 9
    }
100
}
101