Passed
Push — master ( 8987eb...a1b61c )
by Eugene
06:25
created

LegacyCallMiddleware::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace App;
15
16
use Tarantool\Client\Handler\Handler;
17
use Tarantool\Client\Keys;
18
use Tarantool\Client\Middleware\Middleware;
19
use Tarantool\Client\Request\CallRequest;
20
use Tarantool\Client\Request\Request;
21
use Tarantool\Client\Response;
22
23
final class LegacyCallMiddleware implements Middleware
24
{
25
    /** @var bool */
26
    private $useLegacyMarshalling;
27
28
    /**
29
     * @param bool $useLegacyMarshalling
30
     */
31
    private function __construct($useLegacyMarshalling)
32
    {
33
        $this->useLegacyMarshalling = $useLegacyMarshalling;
34
    }
35
36
    public static function legacyResponseMarshalling() : self
37
    {
38
        return new self(true);
39
    }
40
41
    public static function newResponseMarshalling() : self
42
    {
43
        return new self(false);
44
    }
45
46
    public function process(Request $request, Handler $handler) : Response
47
    {
48
        $response = $request instanceof CallRequest
49
            ? $handler->handle(LegacyCallRequest::fromCallRequest($request))
50
            : $handler->handle($request);
51
52
        return $this->useLegacyMarshalling ? $response : new Response([
53
            Keys::CODE => $response->getCode(),
54
            Keys::SYNC => $response->getSync(),
55
            Keys::SCHEMA_ID => $response->getSchemaId(),
56
        ], [
57
            Keys::DATA => $response->getBodyField(Keys::DATA)[0],
58
        ]);
59
    }
60
}
61