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

ExpectationToArrayConverter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 92.68%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 87
ccs 38
cts 41
cp 0.9268
rs 10
c 1
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getScenarioName() 0 7 2
A getScenarioState() 0 7 2
A __construct() 0 6 1
A setResponse() 0 14 2
A getPriority() 0 7 2
A getNewScenarioState() 0 7 2
A convert() 0 18 2
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 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
        if ($expectation->getVersion()->asString() !== '1') {
45
            $expectationArray['version'] = $expectation->getVersion()->asString();
46
        }
47
48 3
        $expectationArray['scenarioName'] = $this->getScenarioName($expectation);
49 3
        $expectationArray['scenarioStateIs'] = $this->getScenarioState($expectation);
50 3
        $expectationArray['newScenarioState'] = $this->getNewScenarioState($expectation);
51 3
        $expectationArray['request'] = $this->requestToArrayConverter->convert($expectation->getRequest());
52
53 3
        $this->setResponse($expectation, $expectationArray);
54
55 3
        $expectationArray['priority'] = $this->getPriority($expectation);
56
57 3
        return $expectationArray;
58
    }
59
60 3
    private function setResponse(Expectation $expectation, array &$expectationArray): void
61
    {
62 3
        $response = $expectation->getResponse();
63
64 3
        if ($response->isHttpResponse()) {
65
            /* @var \Mcustiel\Phiremock\Domain\HttpResponse $response */
66 3
            $expectationArray['response'] = $this->responseConverterLocator
67 3
                ->locate($response)
68 3
                ->convert($response);
69 3
            $expectationArray['proxyTo'] = null;
70
        } else {
71
            /* @var \Mcustiel\Phiremock\Domain\ProxyResponse $response */
72
            $expectationArray['response'] = null;
73
            $expectationArray['proxyTo'] = $response->getUri()->asString();
74
        }
75 3
    }
76
77 3
    private function getScenarioName(Expectation $expectation): ?string
78
    {
79 3
        if ($expectation->hasScenarioName()) {
80 1
            return $expectation->getScenarioName()->asString();
81
        }
82
83 2
        return null;
84
    }
85
86 3
    private function getScenarioState(Expectation $expectation): ?string
87
    {
88 3
        if ($expectation->getRequest()->hasScenarioState()) {
89 1
            return $expectation->getRequest()->getScenarioState()->asString();
90
        }
91
92 2
        return null;
93
    }
94
95 3
    private function getNewScenarioState(Expectation $expectation): ?string
96
    {
97 3
        if ($expectation->getResponse()->hasNewScenarioState()) {
98 1
            return $expectation->getResponse()->getNewScenarioState()->asString();
99
        }
100
101 2
        return null;
102
    }
103
104 3
    private function getPriority(Expectation $expectation): int
105
    {
106 3
        if ($expectation->hasPriority()) {
107 1
            return $expectation->getPriority()->asInt();
108
        }
109
110 2
        return 0;
111
    }
112
}
113