Passed
Push — master ( 9412a8...537d79 )
by Mariano
01:04
created

ExpectationToArrayConverter::getNewScenarioState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

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
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;
20
21
use Mcustiel\Phiremock\Domain\Expectation;
22
23
class ExpectationToArrayConverter
24
{
25
    /** @var RequestConditionToArrayConverterLocator */
26
    private $requestToArrayConverterLocator;
27
28
    /** @var ResponseToArrayConverterLocator */
29
    private $responseConverterLocator;
30
31
    public function __construct(
32
        RequestConditionToArrayConverterLocator $requestConverterLocator,
33
        ResponseToArrayConverterLocator $responseConverterLocator
34
    ) {
35
        $this->requestToArrayConverterLocator = $requestConverterLocator;
36
        $this->responseConverterLocator = $responseConverterLocator;
37
    }
38
39
    public function convert(Expectation $expectation): array
40
    {
41
        $expectationArray = [];
42
43
        if ($expectation->getVersion()->asString() !== '1') {
44
            $expectationArray['version'] = $expectation->getVersion()->asString();
45
        }
46
47
        $expectationArray['scenarioName'] = $this->getScenarioName($expectation);
48
        $expectationArray['scenarioStateIs'] = $this->getScenarioState($expectation);
49
        $expectationArray['newScenarioState'] = $this->getNewScenarioState($expectation);
50
51
        $expectationArray['request'] = $this->requestToArrayConverterLocator
52
            ->locate($expectation)
53
            ->convert($expectation->getRequest());
54
55
        $this->setResponse($expectation, $expectationArray);
56
57
        $expectationArray['priority'] = $this->getPriority($expectation);
58
59
        return $expectationArray;
60
    }
61
62
    private function setResponse(Expectation $expectation, array &$expectationArray): void
63
    {
64
        $response = $expectation->getResponse();
65
66
        if ($response->isHttpResponse()) {
67
            /* @var \Mcustiel\Phiremock\Domain\HttpResponse $response */
68
            $expectationArray['response'] = $this->responseConverterLocator
69
                ->locate($response)
70
                ->convert($response);
71
            $expectationArray['proxyTo'] = null;
72
        } else {
73
            /* @var \Mcustiel\Phiremock\Domain\ProxyResponse $response */
74
            $expectationArray['response'] = null;
75
            $expectationArray['proxyTo'] = $response->getUri()->asString();
76
        }
77
    }
78
79
    private function getScenarioName(Expectation $expectation): ?string
80
    {
81
        if ($expectation->hasScenarioName()) {
82
            return $expectation->getScenarioName()->asString();
83
        }
84
85
        return null;
86
    }
87
88
    private function getScenarioState(Expectation $expectation): ?string
89
    {
90
        if ($expectation->getRequest()->hasScenarioState()) {
91
            return $expectation->getRequest()->getScenarioState()->asString();
92
        }
93
94
        return null;
95
    }
96
97
    private function getNewScenarioState(Expectation $expectation): ?string
98
    {
99
        if ($expectation->getResponse()->hasNewScenarioState()) {
100
            return $expectation->getResponse()->getNewScenarioState()->asString();
101
        }
102
103
        return null;
104
    }
105
106
    private function getPriority(Expectation $expectation): int
107
    {
108
        if ($expectation->hasPriority()) {
109
            return $expectation->getPriority()->asInt();
110
        }
111
112
        return 0;
113
    }
114
}
115