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; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use Mcustiel\Phiremock\Common\Utils\ArrayToExpectationConverterLocator; |
23
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
24
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
25
|
|
|
use Psr\Log\LoggerInterface; |
26
|
|
|
|
27
|
|
|
class RequestToExpectationMapper |
28
|
|
|
{ |
29
|
|
|
const CONTENT_ENCODING_HEADER = 'Content-Encoding'; |
30
|
|
|
|
31
|
|
|
/** @var ArrayToExpectationConverterLocator */ |
32
|
|
|
private $converterLocator; |
33
|
|
|
|
34
|
|
|
/** @var LoggerInterface */ |
35
|
|
|
private $logger; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
ArrayToExpectationConverterLocator $converterLocator, |
39
|
|
|
LoggerInterface $logger |
40
|
|
|
) { |
41
|
|
|
$this->converterLocator = $converterLocator; |
42
|
|
|
$this->logger = $logger; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** @throws Exception */ |
46
|
|
|
public function map(ServerRequestInterface $request): Expectation |
47
|
|
|
{ |
48
|
|
|
$parsedJson = $this->parseJsonBody($request); |
49
|
|
|
$object = $this->converterLocator->locate($parsedJson)->convert($parsedJson); |
50
|
|
|
$this->logger->debug('Parsed expectation: ' . var_export($object, true)); |
51
|
|
|
|
52
|
|
|
return $object; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @throws Exception */ |
56
|
|
|
private function parseJsonBody(ServerRequestInterface $request): array |
57
|
|
|
{ |
58
|
|
|
$this->logger->debug('Adding Expectation->parseJsonBody'); |
59
|
|
|
$body = $request->getBody()->__toString(); |
60
|
|
|
$this->logger->debug($body); |
61
|
|
|
if ($this->hasBinaryBody($request)) { |
62
|
|
|
$body = base64_decode($body, true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$bodyJson = @json_decode($body, true); |
66
|
|
|
if (\JSON_ERROR_NONE !== json_last_error()) { |
67
|
|
|
throw new Exception(json_last_error_msg()); |
68
|
|
|
} |
69
|
|
|
$this->logger->debug('BODY JSON: ' . var_export($bodyJson, true)); |
70
|
|
|
|
71
|
|
|
return $bodyJson; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function hasBinaryBody(ServerRequestInterface $request): bool |
75
|
|
|
{ |
76
|
|
|
return $request->hasHeader(self::CONTENT_ENCODING_HEADER) |
77
|
|
|
&& 'base64' === $request->getHeader(self::CONTENT_ENCODING_HEADER); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|