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

convertBodyCondition()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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