Passed
Push — master ( 537d79...534bb6 )
by Mariano
01:14
created

ExpectationToArrayConverter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 87
rs 10
c 1
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 18 2
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
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\Domain\Expectation;
22
23
class ExpectationToArrayConverter
24
{
25
    /** @var RequestConditionToArrayConverter */
26
    private $requestToArrayConverter;
27
28
    /** @var ResponseToArrayConverterLocator */
29
    private $responseConverterLocator;
30
31
    public function __construct(
32
        RequestConditionToArrayConverter $requestConverter,
33
        ResponseToArrayConverterLocator $responseConverterLocator
34
    ) {
35
        $this->requestToArrayConverter = $requestConverter;
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
        $expectationArray['request'] = $this->requestToArrayConverter->convert($expectation->getRequest());
51
52
        $this->setResponse($expectation, $expectationArray);
53
54
        $expectationArray['priority'] = $this->getPriority($expectation);
55
56
        return $expectationArray;
57
    }
58
59
    private function setResponse(Expectation $expectation, array &$expectationArray): void
60
    {
61
        $response = $expectation->getResponse();
62
63
        if ($response->isHttpResponse()) {
64
            /* @var \Mcustiel\Phiremock\Domain\HttpResponse $response */
65
            $expectationArray['response'] = $this->responseConverterLocator
66
                ->locate($response)
67
                ->convert($response);
68
            $expectationArray['proxyTo'] = null;
69
        } else {
70
            /* @var \Mcustiel\Phiremock\Domain\ProxyResponse $response */
71
            $expectationArray['response'] = null;
72
            $expectationArray['proxyTo'] = $response->getUri()->asString();
73
        }
74
    }
75
76
    private function getScenarioName(Expectation $expectation): ?string
77
    {
78
        if ($expectation->hasScenarioName()) {
79
            return $expectation->getScenarioName()->asString();
80
        }
81
82
        return null;
83
    }
84
85
    private function getScenarioState(Expectation $expectation): ?string
86
    {
87
        if ($expectation->getRequest()->hasScenarioState()) {
88
            return $expectation->getRequest()->getScenarioState()->asString();
89
        }
90
91
        return null;
92
    }
93
94
    private function getNewScenarioState(Expectation $expectation): ?string
95
    {
96
        if ($expectation->getResponse()->hasNewScenarioState()) {
97
            return $expectation->getResponse()->getNewScenarioState()->asString();
98
        }
99
100
        return null;
101
    }
102
103
    private function getPriority(Expectation $expectation): int
104
    {
105
        if ($expectation->hasPriority()) {
106
            return $expectation->getPriority()->asInt();
107
        }
108
109
        return 0;
110
    }
111
}
112