ensureNotInvalidOptionsAreProvided()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
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\ArrayToResponseConverter as ArrayToResponseConverterInterface;
22
use Mcustiel\Phiremock\Domain\Options\Delay;
23
use Mcustiel\Phiremock\Domain\Options\ScenarioState;
24
use Mcustiel\Phiremock\Domain\Response;
25
26
abstract class ArrayToResponseConverter implements ArrayToResponseConverterInterface
27
{
28
    const ALLOWED_OPTIONS = ['delayMillis'=> null];
29
    const NO_DELAY = 0;
30
31 9
    public function convert(array $responseArray): Response
32
    {
33 9
        return $this->convertResponse(
34 9
            $responseArray,
35 9
            $this->getDelay($responseArray),
36 9
            $this->getNewScenarioState($responseArray)
37
        );
38
    }
39
40
    protected function ensureNotInvalidOptionsAreProvided(array $responseArray): void
41
    {
42
        $invalidOptions = array_diff_key($responseArray, static::ALLOWED_OPTIONS);
43
        if (!empty($invalidOptions)) {
44
            throw new \Exception('Unknown expectation options: ' . var_export($invalidOptions, true));
45
        }
46
    }
47
48
    abstract protected function convertResponse(
49
        array $response,
50
        ?Delay $delay,
51
        ?ScenarioState $newScenarioState
52
    ): Response;
53
54 9
    private function getDelay(array $responseArray): ?Delay
55
    {
56 9
        if (empty($responseArray['response'])) {
57 3
            return null;
58
        }
59
60 9
        $responseArray = $responseArray['response'];
61 9
        if (!empty($responseArray['delayMillis'])) {
62 3
            return new Delay($responseArray['delayMillis']);
63
        }
64
65 6
        return null;
66
    }
67
68 9
    private function getNewScenarioState(array $expectationArray): ?ScenarioState
69
    {
70 9
        $newScenarioState = null;
71 9
        if (!empty($expectationArray['newScenarioState'])) {
72 3
            $newScenarioState = new ScenarioState($expectationArray['newScenarioState']);
73
        }
74
75 9
        return $newScenarioState;
76
    }
77
}
78