InteractionCompositor::createResponseFromDTO()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 7
cp 0
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jekccs
5
 * Date: 30.11.18
6
 * Time: 16:18
7
 */
8
9
namespace SmartGamma\Behat\PactExtension\Infrastructure\Interaction;
10
11
12
use PhpPact\Consumer\Model\ConsumerRequest;
13
use PhpPact\Consumer\Model\ProviderResponse;
14
use SmartGamma\Behat\PactExtension\Exception\NoAuthTypeSupported;
15
16
class InteractionCompositor
17
{
18
    /**
19
     * @var MatcherInterface
20
     */
21
    private $matcher;
22
23
    public function __construct(MatcherInterface $matcher)
24
    {
25
        $this->matcher = $matcher;
26
    }
27
28
    /**
29
     * @param InteractionRequestDTO $requestDTO
30
     *
31
     * @return ConsumerRequest
32
     */
33
    public function createRequestFromDTO(InteractionRequestDTO $requestDTO): ConsumerRequest
34
    {
35
        $request = new ConsumerRequest();
36
37
        $request
38
            ->setMethod($requestDTO->getMethod())
39
            ->setPath($requestDTO->getUri());
40
41
        if (null !== $requestDTO->getQuery()) {
42
            $request->setQuery($requestDTO->getQuery());
43
        }
44
45
        foreach ($requestDTO->getHeaders() as $key => $value) {
46
            $request->addHeader($key, $value);
47
        }
48
49
        if (\count($requestDTO->getBody()) > 0) {
50
            $request->setBody($requestDTO->getBody());
51
        }
52
53
        return $request;
54
    }
55
56
    /**
57
     * @param InteractionResponseDTO $responseDTO
58
     *
59
     * @return ProviderResponse
60
     */
61
    public function createResponseFromDTO(InteractionResponseDTO $responseDTO): ProviderResponse
62
    {
63
        $response = new ProviderResponse();
64
        $response
65
            ->setStatus($responseDTO->getStatus());
66
67
        $bodyParameters = $this->buildResponseBodyWithMatchers($responseDTO);
68
69
        if (\count($bodyParameters) > 0) {
70
            $response->setBody($bodyParameters);
71
        }
72
73
        return $response;
74
    }
75
76
    /**
77
     * @param InteractionResponseDTO $responseDTO
78
     *
79
     * @return array
80
     */
81
    private function buildResponseBodyWithMatchers(InteractionResponseDTO $responseDTO): array
82
    {
83
        return array_reduce(
84
            $responseDTO->getRawParameters(),
85
            function(array $carry, array $bodyItem) use ($responseDTO){
86
87
                $matchType = $bodyItem['match'] ? $bodyItem['match'] : MatcherInterface::EXACT_TYPE;
88
                $value = $matchType == MatcherInterface::EACH_LIKE_TYPE ? $responseDTO->getMatchingObjectStructure($bodyItem['value']) : $bodyItem['value'];
89
90
                if ('null' !== $value) {
91
                    $carry[$bodyItem['parameter']] = $this->matcher->$matchType($value);
92
                }
93
94
                return $carry;
95
            },
96
            []
97
        );
98
    }
99
}
100