RequestToExpectationMapper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 51
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasBinaryBody() 0 4 2
A parseJsonBody() 0 16 3
A map() 0 7 1
A __construct() 0 6 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;
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