getResponseWithReplacedBody()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 20
rs 9.9332
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\Common\StringStream;
22
use Mcustiel\Phiremock\Domain\Expectation;
23
use Mcustiel\Phiremock\Domain\Http\BinaryBody;
24
use Mcustiel\Phiremock\Domain\HttpResponse;
25
use Mcustiel\Phiremock\Server\Model\ScenarioStorage;
26
use Mcustiel\Phiremock\Server\Utils\Strategies\Utils\RegexReplacer;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Log\LoggerInterface;
30
31
class RegexResponseStrategy extends AbstractResponse implements ResponseStrategyInterface
32
{
33
    /** @var RegexReplacer */
34
    private $regexReplacer;
35
36
    public function __construct(
37
        ScenarioStorage $scenarioStorage,
38
        LoggerInterface $logger,
39
        RegexReplacer $regexReplacer
40
    ) {
41
        parent::__construct($scenarioStorage, $logger);
42
43
        $this->regexReplacer = $regexReplacer;
44
    }
45
46
    public function createResponse(Expectation $expectation, ResponseInterface $httpResponse, ServerRequestInterface $request): ResponseInterface
47
    {
48
        $httpResponse = $this->getResponseWithReplacedBody(
49
            $expectation,
50
            $httpResponse,
51
            $request
52
        );
53
        $httpResponse = $this->getResponseWithReplacedHeaders(
54
            $expectation,
55
            $httpResponse,
56
            $request
57
        );
58
        /** @var HttpResponse $responseConfig */
59
        $responseConfig = $expectation->getResponse();
60
        $httpResponse = $this->getResponseWithStatusCode($responseConfig, $httpResponse);
61
        $this->processScenario($expectation);
62
        $this->processDelay($responseConfig);
63
64
        return $httpResponse;
65
    }
66
67
    private function getResponseWithReplacedBody(
68
        Expectation $expectation,
69
        ResponseInterface $httpResponse,
70
        ServerRequestInterface $httpRequest
71
    ): ResponseInterface {
72
        /** @var HttpResponse $responseConfig */
73
        $responseConfig = $expectation->getResponse();
74
75
        if ($responseConfig->hasBody()) {
76
            if ($responseConfig->getBody() instanceof BinaryBody) {
77
                $httpResponse = $httpResponse->withBody($responseConfig->getBody()->asStream());
78
            } else {
79
                $bodyString = $responseConfig->getBody()->asString();
80
                $bodyString = $this->regexReplacer->fillWithUrlMatches($expectation, $httpRequest, $bodyString);
81
                $bodyString = $this->regexReplacer->fillWithBodyMatches($expectation, $httpRequest, $bodyString);
82
                $httpResponse = $httpResponse->withBody(new StringStream($bodyString));
83
            }
84
        }
85
86
        return $httpResponse;
87
    }
88
89
    private function getResponseWithReplacedHeaders(
90
        Expectation $expectation,
91
        ResponseInterface $httpResponse,
92
        ServerRequestInterface $httpRequest
93
    ): ResponseInterface {
94
        /** @var HttpResponse $responseConfig */
95
        $responseConfig = $expectation->getResponse();
96
        $headers = $responseConfig->getHeaders();
97
98
        if ($headers === null || $headers->isEmpty()) {
99
            return $httpResponse;
100
        }
101
102
        foreach ($headers as $header) {
103
            $headerValue = $header->getValue()->asString();
104
            $headerValue = $this->regexReplacer->fillWithUrlMatches($expectation, $httpRequest, $headerValue);
105
            $headerValue = $this->regexReplacer->fillWithBodyMatches($expectation, $httpRequest, $headerValue);
106
            $httpResponse = $httpResponse->withHeader($header->getName()->asString(), $headerValue);
107
        }
108
109
        return $httpResponse;
110
    }
111
}
112