ProxyResponseStrategy   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createResponse() 0 14 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 Laminas\Diactoros\Uri;
22
use Mcustiel\Phiremock\Domain\Expectation;
23
use Mcustiel\Phiremock\Domain\ProxyResponse;
24
use Mcustiel\Phiremock\Server\Model\ScenarioStorage;
25
use Psr\Http\Client\ClientInterface;
26
use Psr\Http\Message\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Psr\Log\LoggerInterface;
29
30
class ProxyResponseStrategy extends AbstractResponse implements ResponseStrategyInterface
31
{
32
    /** @var ClientInterface */
33
    private $httpService;
34
35
    public function __construct(
36
        ScenarioStorage $scenarioStorage,
37
        LoggerInterface $logger,
38
        ClientInterface $httpService
39
    ) {
40
        parent::__construct($scenarioStorage, $logger);
41
        $this->httpService = $httpService;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @see \Mcustiel\Phiremock\Server\Utils\Strategies\ResponseStrategyInterface::createResponse()
48
     */
49
    public function createResponse(
50
        Expectation $expectation,
51
        ResponseInterface $transactionData,
52
        ServerRequestInterface $request
53
    ): ResponseInterface {
54
        /** @var ProxyResponse $response */
55
        $response = $expectation->getResponse();
56
        $url = $response->getUri()->asString();
57
        $this->logger->debug('Proxying request to : ' . $url);
58
        $this->processScenario($expectation);
59
        $this->processDelay($response);
60
61
        return $this->httpService->sendRequest(
62
            $request->withUri(new Uri($url))
63
        );
64
    }
65
}
66