Passed
Push — master ( a33cf4...6335da )
by Mariano
01:54
created

convertUrlCondition()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 18
ccs 9
cts 11
cp 0.8182
crap 4.0961
rs 9.9332
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\V2;
20
21
use Mcustiel\Phiremock\Common\Utils\ArrayToRequestConditionConverter as ArrayToRequestConditionConverterInterface;
22
use Mcustiel\Phiremock\Common\Utils\V1\ArrayToRequestConditionConverter as ArrayToRequestConditionConverterV1;
23
use Mcustiel\Phiremock\Domain\Condition\Conditions\BodyCondition;
24
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderCondition;
25
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionCollection;
26
use Mcustiel\Phiremock\Domain\Condition\Conditions\HeaderConditionIterator;
27
use Mcustiel\Phiremock\Domain\Condition\Conditions\MethodCondition;
28
use Mcustiel\Phiremock\Domain\Condition\Conditions\UrlCondition;
29
use Mcustiel\Phiremock\Domain\Condition\Matchers\MatcherFactory;
30
use Mcustiel\Phiremock\Domain\Conditions;
31
use Mcustiel\Phiremock\Domain\Http\HeaderName;
32
use Mcustiel\Phiremock\Domain\Options\ScenarioState;
33
34
class ArrayToRequestConditionConverter implements ArrayToRequestConditionConverterInterface //  extends ArrayToRequestConditionConverterV1
35
{
36
    const ALLOWED_OPTIONS = [
37
        'scenarioStateIs' => null,
38
        'method'          => null,
39
        'url'             => null,
40
        'body'            => null,
41
        'headers'         => null,
42
    ];
43
44
    /** @var MatcherFactory */
45
    private $matcherFactory;
46
47 3
    public function __construct()
48
    {
49 3
        $this->matcherFactory = new MatcherFactory();
50 3
    }
51
52 3
    public function convert(array $requestArray): Conditions
53
    {
54 3
        $this->ensureNotInvalidOptionsAreProvided($requestArray);
55
56 3
        return new Conditions(
57 3
            $this->convertMethodCondition($requestArray),
58 3
            $this->convertUrlCondition($requestArray),
59 3
            $this->convertBodyCondition($requestArray),
60 3
            $this->convertHeadersConditions($requestArray),
61 3
            $this->convertScenarioState($requestArray)
62
        );
63
    }
64
65 3
    protected function ensureNotInvalidOptionsAreProvided(array $requestArray): void
66
    {
67 3
        $invalidOptions = array_diff_key($requestArray, static::ALLOWED_OPTIONS);
68 3
        if (!empty($invalidOptions)) {
69
            throw new \Exception('Unknown request conditions: ' . var_export($invalidOptions, true));
70
        }
71 3
    }
72
73 3
    protected function convertHeadersConditions(array $requestArray): ?HeaderConditionIterator
74
    {
75 3
        if (!empty($requestArray['headers'])) {
76 1
            $headers = $requestArray['headers'];
77 1
            if (!\is_array($headers)) {
78
                throw new \InvalidArgumentException('Headers condition is invalid: ' . var_export($headers, true));
79
            }
80 1
            $headersCollection = new HeaderConditionCollection();
81 1
            foreach ($headers as $headerName => $header) {
82 1
                $headersCollection->setHeaderCondition(
83 1
                    new HeaderName($headerName),
84 1
                    $this->convertHeaderCondition($header)
85
                );
86
            }
87
88 1
            return $headersCollection->iterator();
89
        }
90
91 2
        return null;
92
    }
93
94 1
    protected function convertHeaderCondition($headerCondition): HeaderCondition
95
    {
96 1
        if (!\is_array($headerCondition)) {
97
            throw new \InvalidArgumentException('Headers condition is invalid: ' . var_export($headerCondition, true));
98
        }
99 1
        $value = current($headerCondition);
100 1
        if (!\is_string($value)) {
101
            throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value));
102
        }
103
104 1
        return new HeaderCondition(
105 1
            $this->matcherFactory->createFrom(key($headerCondition), $value)
106
        );
107
    }
108
109 3
    protected function convertUrlCondition(array $requestArray): ?UrlCondition
110
    {
111 3
        if (!empty($requestArray['url'])) {
112 1
            $urlCondition = $requestArray['url'];
113 1
            if (!\is_array($urlCondition)) {
114
                throw new \InvalidArgumentException('Url condition is invalid: ' . var_export($urlCondition, true));
115
            }
116 1
            $value = current($urlCondition);
117 1
            if (!\is_string($value)) {
118
                throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value));
119
            }
120
121 1
            return new UrlCondition(
122 1
                $this->matcherFactory->createFrom(key($urlCondition), $value)
123
                );
124
        }
125
126 2
        return null;
127
    }
128
129 3
    protected function convertBodyCondition(array $requestArray): ?BodyCondition
130
    {
131 3
        if (!empty($requestArray['body'])) {
132 2
            $bodyCondition = $requestArray['body'];
133 2
            if (!\is_array($bodyCondition)) {
134
                throw new \InvalidArgumentException('Body condition is invalid: ' . var_export($bodyCondition, true));
135
            }
136 2
            $value = current($bodyCondition);
137 2
            if (!\is_string($value)) {
138
                throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value));
139
            }
140
141 2
            return new BodyCondition(
142 2
                $this->matcherFactory->createFrom(key($bodyCondition), $value)
143
            );
144
        }
145
146 1
        return null;
147
    }
148
149 3
    protected function convertScenarioState(array $requestArray): ?ScenarioState
150
    {
151 3
        if (!empty($requestArray['scenarioStateIs'])) {
152 1
            return new ScenarioState($requestArray['scenarioStateIs']);
153
        }
154
155 2
        return null;
156
    }
157
158 3
    protected function getMatcherFactory(): MatcherFactory
159
    {
160 3
        return $this->matcherFactory;
161
    }
162
163 3
    protected function convertMethodCondition(array $requestArray): ?MethodCondition
164
    {
165 3
        if (!empty($requestArray['method'])) {
166 3
            $methodCondition = $requestArray['method'];
167 3
            if (!\is_array($methodCondition)) {
168
                throw new \InvalidArgumentException('Method condition is invalid: ' . var_export($methodCondition, true));
169
            }
170 3
            $value = current($methodCondition);
171 3
            if (!\is_string($value)) {
172
                throw new \InvalidArgumentException('Invalid condition value. Expected string, got: ' . \gettype($value));
173
            }
174
175 3
            return new MethodCondition(
176 3
                $this->getMatcherFactory()->createFrom(key($methodCondition), $value)
177
            );
178
        }
179
180
        return null;
181
    }
182
}
183