Completed
Push — master ( ee6bc9...3f15df )
by Eugene
01:30
created

SpyMiddleware::getTraceLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 Tarantool\Client\Tests;
15
16
use Tarantool\Client\Handler\Handler;
17
use Tarantool\Client\Middleware\Middleware;
18
use Tarantool\Client\Request\Request;
19
use Tarantool\Client\Response;
20
21
final class SpyMiddleware implements Middleware
22
{
23
    private $traceLog;
24
    private $getTraceValue;
25
26
    private function __construct(\Closure $getTraceValue, ?\ArrayObject $traceLog = null)
27
    {
28
        $this->getTraceValue = $getTraceValue;
29
        $this->traceLog = $traceLog ?: new \ArrayObject();
30
    }
31
32
    public static function fromTraceId($traceId, ?\ArrayObject $traceLog = null) : self
33
    {
34
        return new self(static function () use ($traceId) {
35
            return $traceId;
36
        }, $traceLog);
37
    }
38
39
    public function process(Request $request, Handler $handler) : Response
40
    {
41
        $this->traceLog->append(($this->getTraceValue)());
42
43
        return $handler->handle($request);
44
    }
45
46
    public function getTraceLog() : \ArrayObject
47
    {
48
        return $this->traceLog;
49
    }
50
51
    public function getTraceLogArray() : array
52
    {
53
        return $this->traceLog->getArrayCopy();
54
    }
55
}
56