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

SpyMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A fromTraceId() 0 6 1
A process() 0 6 1
A getTraceLog() 0 4 1
A getTraceLogArray() 0 4 1
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