ExpectationToArrayConverter::getScenarioName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

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