convertFormDataConditions()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
cc 4
eloc 11
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.9
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\ArrayToRequestConditionConverter as ArrayToRequestConditionConverterInterface;
22
use Mcustiel\Phiremock\Domain\Condition\Conditions\BodyCondition;
23
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormDataCondition;
24
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormFieldCondition;
25
use Mcustiel\Phiremock\Domain\Condition\Conditions\FormFieldConditionIterator;
26
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderCondition;
27
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionCollection;
28
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionIterator;
29
use Mcustiel\Phiremock\Domain\Condition\Conditions\MethodCondition;
30
use Mcustiel\Phiremock\Domain\Condition\Conditions\UrlCondition;
31
use Mcustiel\Phiremock\Domain\Condition\Matchers\MatcherFactory;
32
use Mcustiel\Phiremock\Domain\Condition\MatchersEnum;
33
use Mcustiel\Phiremock\Domain\Conditions;
34
use Mcustiel\Phiremock\Domain\Http\FormFieldName;
35
use Mcustiel\Phiremock\Domain\Http\HeaderName;
36
use Mcustiel\Phiremock\Domain\Options\ScenarioState;
37
38
class ArrayToRequestConditionConverter implements ArrayToRequestConditionConverterInterface
39
{
40
    const ALLOWED_OPTIONS = [
41
        'method'   => null,
42
        'url'      => null,
43
        'body'     => null,
44
        'headers'  => null,
45
        'formData' => null,
46
    ];
47
48
    /** @var MatcherFactory */
49
    private $matcherFactory;
50
51 9
    public function __construct()
52
    {
53 9
        $this->matcherFactory = new MatcherFactory();
54 9
    }
55
56 9
    public function convert(array $requestArray): Conditions
57
    {
58 9
        $this->ensureNotInvalidOptionsAreProvided($requestArray);
59
60 9
        return new Conditions(
61 9
            $this->convertMethodCondition($requestArray['request']),
62 9
            $this->convertUrlCondition($requestArray['request']),
63 9
            $this->convertBodyCondition($requestArray['request']),
64 9
            $this->convertHeadersConditions($requestArray['request']),
65 9
            $this->convertFormDataConditions($requestArray['request']),
66 9
            $this->convertScenarioState($requestArray)
67
        );
68
    }
69
70 9
    protected function ensureNotInvalidOptionsAreProvided(array $requestArray): void
71
    {
72 9
        $invalidOptions = array_diff_key($requestArray['request'], static::ALLOWED_OPTIONS);
73 9
        if (!empty($invalidOptions)) {
74
            throw new \Exception('Unknown request conditions: ' . var_export($invalidOptions, true));
75
        }
76 9
    }
77
78 9
    protected function convertHeadersConditions(array $requestArray): ?HeaderConditionIterator
79
    {
80 9
        if (!empty($requestArray['headers'])) {
81 3
            $headers = $requestArray['headers'];
82 3
            if (!\is_array($headers)) {
83
                throw new \InvalidArgumentException('Headers condition is invalid: ' . var_export($headers, true));
84
            }
85 3
            $headersCollection = new HeaderConditionCollection();
86 3
            foreach ($headers as $headerName => $header) {
87 3
                $headersCollection->setHeaderCondition(
88 3
                    new HeaderName($headerName),
89 3
                    $this->convertHeaderCondition($header)
90
                );
91
            }
92
93 3
            return $headersCollection->iterator();
94
        }
95
96 6
        return null;
97
    }
98
99 9
    protected function convertFormDataConditions(array $requestArray): ?FormFieldConditionIterator
100
    {
101 9
        if (!empty($requestArray['formData'])) {
102 3
            $formFields = $requestArray['formData'];
103 3
            if (!\is_array($formFields)) {
104
                throw new \InvalidArgumentException('Form data condition is invalid: ' . var_export($formFields, true));
105
            }
106 3
            $formData = new FormDataCondition();
107 3
            foreach ($formFields as $formFieldName => $condition) {
108 3
                $formData->setFieldCondition(
109 3
                    new FormFieldName($formFieldName),
110 3
                    $this->convertFormFieldCondition($condition)
111
                 );
112
            }
113
114 3
            return $formData->iterator();
115
        }
116
117 6
        return null;
118
    }
119
120 3
    protected function convertHeaderCondition($headerCondition): HeaderCondition
121
    {
122 3
        if (!\is_array($headerCondition)) {
123
            throw new \InvalidArgumentException('Headers condition is invalid: ' . var_export($headerCondition, true));
124
        }
125 3
        $value = current($headerCondition);
126 3
        if (!\is_string($value)) {
127
            throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value));
128
        }
129
130 3
        return new HeaderCondition(
131 3
            $this->matcherFactory->createFrom(key($headerCondition), $value)
132
        );
133
    }
134
135 3
    protected function convertFormFieldCondition($fieldCondition): FormFieldCondition
136
    {
137 3
        if (!\is_array($fieldCondition)) {
138
            throw new \InvalidArgumentException('Form field condition is invalid: ' . var_export($fieldCondition, true));
139
        }
140 3
        $value = current($fieldCondition);
141 3
        if (!\is_string($value)) {
142
            throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value));
143
        }
144
145 3
        return new FormFieldCondition(
146 3
            $this->matcherFactory->createFrom(key($fieldCondition), $value)
147
        );
148
    }
149
150 9
    protected function convertUrlCondition(array $requestArray): ?UrlCondition
151
    {
152 9
        if (!empty($requestArray['url'])) {
153 3
            $urlCondition = $requestArray['url'];
154 3
            if (!\is_array($urlCondition)) {
155
                throw new \InvalidArgumentException('Url condition is invalid: ' . var_export($urlCondition, true));
156
            }
157 3
            $value = current($urlCondition);
158 3
            if (!\is_string($value)) {
159
                throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value));
160
            }
161
162 3
            return new UrlCondition(
163 3
                $this->matcherFactory->createFrom(key($urlCondition), $value)
164
            );
165
        }
166
167 6
        return null;
168
    }
169
170 9
    protected function convertMethodCondition(array $requestArray): ?MethodCondition
171
    {
172 9
        if (!empty($requestArray['method'])) {
173 9
            $method = $requestArray['method'];
174 9
            if (!\is_string($method)) {
175
                throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($method));
176
            }
177
178 9
            return new MethodCondition(
179 9
                $this->matcherFactory->createFrom(MatchersEnum::SAME_STRING, $method)
180
            );
181
        }
182
183
        return null;
184
    }
185
186 9
    protected function convertBodyCondition(array $requestArray): ?BodyCondition
187
    {
188 9
        if (!empty($requestArray['body'])) {
189 6
            $bodyCondition = $requestArray['body'];
190 6
            if (!\is_array($bodyCondition)) {
191
                throw new \InvalidArgumentException('Body condition is invalid: ' . var_export($bodyCondition, true));
192
            }
193 6
            $value = current($bodyCondition);
194 6
            if (!\is_string($value)) {
195
                throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value));
196
            }
197
198 6
            return new BodyCondition(
199 6
                $this->matcherFactory->createFrom(key($bodyCondition), $value)
200
            );
201
        }
202
203 3
        return null;
204
    }
205
206 9
    protected function convertScenarioState(array $requestArray): ?ScenarioState
207
    {
208 9
        if (!empty($requestArray['scenarioStateIs'])) {
209 3
            return new ScenarioState($requestArray['scenarioStateIs']);
210
        }
211
212 6
        return null;
213
    }
214
215
    protected function getMatcherFactory(): MatcherFactory
216
    {
217
        return $this->matcherFactory;
218
    }
219
}
220