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

ExpectationToArrayConverter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 63.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 81
ccs 23
cts 36
cp 0.6389
rs 10
c 1
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewScenarioState() 0 7 2
A setResponse() 0 9 2
A __construct() 0 6 1
A convert() 0 17 1
A getPriority() 0 7 2
A getScenarioName() 0 7 2
A getScenarioState() 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\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
    private function setResponse(Expectation $expectation, array &$expectationArray): void
0 ignored issues
show
Unused Code introduced by
The method setResponse() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
60
    {
61
        $response = $expectation->getResponse();
62
63
        if ($response->isHttpResponse()) {
64
            /* @var \Mcustiel\Phiremock\Domain\HttpResponse $response */
65
        } else {
66
            /* @var \Mcustiel\Phiremock\Domain\ProxyResponse $response */
67
            $expectationArray['response'] = null;
68
        }
69
    }
70
71 3
    private function getScenarioName(Expectation $expectation): ?string
72
    {
73 3
        if ($expectation->hasScenarioName()) {
74 1
            return $expectation->getScenarioName()->asString();
75
        }
76
77 2
        return null;
78
    }
79
80
    private function getScenarioState(Expectation $expectation): ?string
81
    {
82
        if ($expectation->getRequest()->hasScenarioState()) {
83
            return $expectation->getRequest()->getScenarioState()->asString();
84
        }
85
86
        return null;
87
    }
88
89
    private function getNewScenarioState(Expectation $expectation): ?string
90
    {
91
        if ($expectation->getResponse()->hasNewScenarioState()) {
92
            return $expectation->getResponse()->getNewScenarioState()->asString();
93
        }
94
95
        return null;
96
    }
97
98 3
    private function getPriority(Expectation $expectation): int
99
    {
100 3
        if ($expectation->hasPriority()) {
101 1
            return $expectation->getPriority()->asInt();
102
        }
103
104 2
        return 0;
105
    }
106
}
107