Passed
Push — master ( 6335da...f14e16 )
by Mariano
01:55
created

ExpectationToArrayConverter::convert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 1
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\V2;
20
21
use Mcustiel\Phiremock\Common\Utils\V1\ExpectationToArrayConverter as ExpectationToArrayConverterV1;
22
use Mcustiel\Phiremock\Domain\Expectation;
23
24
class ExpectationToArrayConverter extends ExpectationToArrayConverterV1
25
{
26
    /** @var RequestConditionToArrayConverter */
27
    private $requestToArrayConverter;
28
29
    /** @var ResponseToArrayConverterLocator */
30
    private $responseConverterLocator;
31
32 3
    public function __construct(
33
        RequestConditionToArrayConverter $requestConverter,
34
        ResponseToArrayConverterLocator $responseConverterLocator
35
    ) {
36 3
        $this->requestToArrayConverter = $requestConverter;
37 3
        $this->responseConverterLocator = $responseConverterLocator;
38 3
    }
39
40 3
    public function convert(Expectation $expectation): array
41
    {
42 3
        $expectationArray = [];
43
44 3
        $expectationArray['version'] = $expectation->getVersion()->asString();
45
46 3
        $expectationArray['scenarioName'] = $this->getScenarioName($expectation);
47 3
        $expectationArray['on'] = $this->requestToArrayConverter->convert($expectation->getRequest());
48
49 3
        $response = $expectation->getResponse();
50 3
        $expectationArray['then'] = $this->responseConverterLocator
51 3
            ->locate($response)
52 3
            ->convert($response);
53
54 3
        $expectationArray['priority'] = $this->getPriority($expectation);
55
56 3
        return $expectationArray;
57
    }
58
59 3
    private function getScenarioName(Expectation $expectation): ?string
60
    {
61 3
        if ($expectation->hasScenarioName()) {
62 1
            return $expectation->getScenarioName()->asString();
63
        }
64
65 2
        return null;
66
    }
67
68
    private function getScenarioState(Expectation $expectation): ?string
69
    {
70
        if ($expectation->getRequest()->hasScenarioState()) {
71
            return $expectation->getRequest()->getScenarioState()->asString();
72
        }
73
74
        return null;
75
    }
76
77
    private function getNewScenarioState(Expectation $expectation): ?string
78
    {
79
        if ($expectation->getResponse()->hasNewScenarioState()) {
80
            return $expectation->getResponse()->getNewScenarioState()->asString();
81
        }
82
83
        return null;
84
    }
85
86 3
    private function getPriority(Expectation $expectation): int
87
    {
88 3
        if ($expectation->hasPriority()) {
89 1
            return $expectation->getPriority()->asInt();
90
        }
91
92 2
        return 0;
93
    }
94
}
95