HttpResponseStrategy   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 31
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponseWithBody() 0 7 2
A createResponse() 0 15 1
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\Server\Utils\Strategies;
20
21
use Mcustiel\Phiremock\Domain\Expectation;
22
use Mcustiel\Phiremock\Domain\HttpResponse;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
26
class HttpResponseStrategy extends AbstractResponse implements ResponseStrategyInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @see \Mcustiel\Phiremock\Server\Utils\Strategies\ResponseStrategyInterface::createResponse()
32
     */
33
    public function createResponse(
34
        Expectation $expectation,
35
        ResponseInterface $httpResponse,
36
        ServerRequestInterface $request
37
    ): ResponseInterface {
38
        /** @var HttpResponse $responseConfig */
39
        $responseConfig = $expectation->getResponse();
40
41
        $httpResponse = $this->getResponseWithBody($responseConfig, $httpResponse);
42
        $httpResponse = $this->getResponseWithStatusCode($responseConfig, $httpResponse);
43
        $httpResponse = $this->getResponseWithHeaders($responseConfig, $httpResponse);
44
        $this->processScenario($expectation);
45
        $this->processDelay($responseConfig);
46
47
        return $httpResponse;
48
    }
49
50
    private function getResponseWithBody(HttpResponse $responseConfig, ResponseInterface $httpResponse): ResponseInterface
51
    {
52
        if ($responseConfig->getBody()) {
53
            $httpResponse = $httpResponse->withBody($responseConfig->getBody()->asStream());
54
        }
55
56
        return $httpResponse;
57
    }
58
}
59