ExpectationToArrayConverter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 74.19%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 69
ccs 23
cts 31
cp 0.7419
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A convert() 0 17 1
A getNewScenarioState() 0 7 2
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 15
    public function __construct(
33
        RequestConditionToArrayConverter $requestConverter,
34
        ResponseToArrayConverterLocator $responseConverterLocator
35
    ) {
36 15
        $this->requestToArrayConverter = $requestConverter;
37 15
        $this->responseConverterLocator = $responseConverterLocator;
38 15
    }
39
40 15
    public function convert(Expectation $expectation): array
41
    {
42 15
        $expectationArray = [];
43
44 15
        $expectationArray['version'] = $expectation->getVersion()->asString();
45
46 15
        $expectationArray['scenarioName'] = $this->getScenarioName($expectation);
47 15
        $expectationArray['on'] = $this->requestToArrayConverter->convert($expectation->getRequest());
48
49 15
        $response = $expectation->getResponse();
50 15
        $expectationArray['then'] = $this->responseConverterLocator
51 15
            ->locate($response)
52 15
            ->convert($response);
53
54 15
        $expectationArray['priority'] = $this->getPriority($expectation);
55
56 15
        return $expectationArray;
57
    }
58
59 15
    private function getScenarioName(Expectation $expectation): ?string
60
    {
61 15
        if ($expectation->hasScenarioName()) {
62 3
            return $expectation->getScenarioName()->asString();
63
        }
64
65 12
        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 15
    private function getPriority(Expectation $expectation): int
87
    {
88 15
        if ($expectation->hasPriority()) {
89 3
            return $expectation->getPriority()->asInt();
90
        }
91
92 12
        return 0;
93
    }
94
}
95