Passed
Pull Request — master (#15)
by Mariano
40:28
created

RegexProxyResponseStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 4
dl 0
loc 9
rs 10
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 Laminas\Diactoros\Uri;
22
use Mcustiel\Phiremock\Domain\Expectation;
23
use Mcustiel\Phiremock\Domain\ProxyResponse;
24
use Mcustiel\Phiremock\Server\Model\ScenarioStorage;
25
use Mcustiel\Phiremock\Server\Utils\Strategies\Utils\RegexReplacer;
26
use Psr\Http\Client\ClientInterface;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Log\LoggerInterface;
30
31
class RegexProxyResponseStrategy extends AbstractResponse implements ResponseStrategyInterface
32
{
33
    /** @var ClientInterface */
34
    private $httpService;
35
    /** @var RegexReplacer */
36
    private $regexReplacer;
37
38
    public function __construct(
39
        ScenarioStorage $scenarioStorage,
40
        LoggerInterface $logger,
41
        ClientInterface $httpService,
42
        RegexReplacer $regexReplacer
43
    ) {
44
        parent::__construct($scenarioStorage, $logger);
45
        $this->httpService = $httpService;
46
        $this->regexReplacer = $regexReplacer;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * @see \Mcustiel\Phiremock\Server\Utils\Strategies\ResponseStrategyInterface::createResponse()
53
     */
54
    public function createResponse(
55
        Expectation $expectation,
56
        ResponseInterface $transactionData,
57
        ServerRequestInterface $request
58
    ): ResponseInterface {
59
        /** @var ProxyResponse $response */
60
        $response = $expectation->getResponse();
61
        $url = $response->getUri()->asString();
62
        $url = $this->regexReplacer->fillWithUrlMatches($expectation, $request, $url);
63
        $url = $this->regexReplacer->fillWithBodyMatches($expectation, $request, $url);
64
        $this->logger->debug('Proxying request to : ' . $url);
65
        $this->processScenario($expectation);
66
        $this->processDelay($response);
67
68
        return $this->httpService->sendRequest(
69
            $request->withUri(new Uri($url))
70
        );
71
    }
72
}
73