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

ArrayToExpectationConverter::convertRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
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\ArrayToExpectationConverter as ArrayToExpectationConverterInterface;
22
use Mcustiel\Phiremock\Domain\Conditions;
23
use Mcustiel\Phiremock\Domain\Expectation;
24
use Mcustiel\Phiremock\Domain\Http\StatusCode;
25
use Mcustiel\Phiremock\Domain\HttpResponse;
26
use Mcustiel\Phiremock\Domain\Options\Priority;
27
use Mcustiel\Phiremock\Domain\Options\ScenarioName;
28
use Mcustiel\Phiremock\Domain\Response;
29
use Mcustiel\Phiremock\Domain\Version;
30
31
class ArrayToExpectationConverter implements ArrayToExpectationConverterInterface
32
{
33
    const ALLOWED_OPTIONS = [
34
        'version'         => null,
35
        'scenarioName'    => null,
36
        'scenarioStateIs' => null,
37
        'newScenarioState'=> null,
38
        'priority'        => null,
39
        'proxyTo'         => null,
40
        'request'         => null,
41
        'response'        => null,
42
    ];
43
44
    /** @var ArrayToRequestConditionConverter */
45
    private $arrayToConditionsConverter;
46
    /** @var ArrayToResponseConverterLocator */
47
    private $arrayToResponseConverterLocator;
48
49 3
    public function __construct(
50
        ArrayToRequestConditionConverter $arrayToConditionsConverter,
51
        ArrayToResponseConverterLocator $arrayToResponseConverterLocator
52
    ) {
53 3
        $this->arrayToConditionsConverter = $arrayToConditionsConverter;
54 3
        $this->arrayToResponseConverterLocator = $arrayToResponseConverterLocator;
55 3
    }
56
57 3
    public function convert(array $expectationArray): Expectation
58
    {
59 3
        $this->ensureNotInvalidOptionsAreProvided($expectationArray);
60 3
        $version = $this->getVersion($expectationArray);
61 3
        $request = $this->convertRequest($expectationArray);
62 3
        $response = $this->convertResponse($expectationArray);
63 3
        $scenarioName = $this->getScenarioName($expectationArray);
64 3
        $priority = $this->getPriority($expectationArray);
65
66 3
        return new Expectation($request, $response, $scenarioName, $priority, $version);
67
    }
68
69 3
    private function ensureNotInvalidOptionsAreProvided(array $expectationArray): void
70
    {
71 3
        $invalidOptions = array_diff_key($expectationArray, self::ALLOWED_OPTIONS);
72 3
        if (!empty($invalidOptions)) {
73
            throw new \Exception('Unknown expectation options: ' . var_export($invalidOptions, true));
74
        }
75 3
    }
76
77 3
    private function getVersion(array $expectationArray): Version
78
    {
79 3
        if (isset($expectationArray['version'])) {
80
            return new Version((int) $expectationArray['version']);
81
        }
82
83 3
        return new Version(1);
84
    }
85
86 3
    private function getPriority(array $expectationArray): ?Priority
87
    {
88 3
        $priority = null;
89 3
        if (!empty($expectationArray['priority'])) {
90 1
            $priority = new Priority((int) $expectationArray['priority']);
91
        }
92
93 3
        return $priority;
94
    }
95
96 3
    private function getScenarioName(array $expectationArray): ?ScenarioName
97
    {
98 3
        $scenarioName = null;
99 3
        if (!empty($expectationArray['scenarioName'])) {
100 1
            $scenarioName = new ScenarioName($expectationArray['scenarioName']);
101
        }
102
103 3
        return $scenarioName;
104
    }
105
106 3
    private function convertResponse(array $expectationArray): Response
107
    {
108 3
        if (!isset($expectationArray['response']) && !isset($expectationArray['proxyTo'])) {
109
            return new HttpResponse(new StatusCode(200), null, null, null, null);
110
        }
111 3
        if (!isset($expectationArray['proxyTo']) && !\is_array($expectationArray['response'])) {
112
            throw new \InvalidArgumentException('Invalid response definition: ' . var_export($expectationArray['response'], true));
113
        }
114
115 3
        return $this->arrayToResponseConverterLocator
116 3
            ->locate($expectationArray)
117 3
            ->convert($expectationArray);
118
    }
119
120 3
    private function convertRequest(array $expectationArray): Conditions
121
    {
122 3
        if (!isset($expectationArray['request'])) {
123
            throw new \InvalidArgumentException('Expectation request is not set');
124
        }
125
126 3
        return $this->arrayToConditionsConverter->convert($expectationArray);
127
    }
128
}
129