LoggingMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 37
ccs 0
cts 22
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A process() 0 28 2
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\Middleware;
15
16
use Psr\Log\LoggerInterface;
17
use Tarantool\Client\Handler\Handler;
18
use Tarantool\Client\Request\Request;
19
use Tarantool\Client\RequestTypes;
20
use Tarantool\Client\Response;
21
22
final class LoggingMiddleware implements Middleware
23
{
24
    private $logger;
25
26
    public function __construct(LoggerInterface $logger)
27
    {
28
        $this->logger = $logger;
29
    }
30
31
    public function process(Request $request, Handler $handler) : Response
32
    {
33
        $requestName = RequestTypes::getName($request->getType());
34
35
        $this->logger->debug("Starting handling request \"$requestName\"", [
36
            'request' => $request,
37
        ]);
38
39
        $start = \microtime(true);
40
        try {
41
            $response = $handler->handle($request);
42
        } catch (\Throwable $e) {
43
            $this->logger->error("Request \"$requestName\" failed", [
44
                'request' => $request,
45
                'exception' => $e,
46
                'duration_ms' => \round((\microtime(true) - $start) * 1000),
47
            ]);
48
49
            throw $e;
50
        }
51
52
        $this->logger->debug("Finished handling request \"$requestName\"", [
53
            'request' => $request,
54
            'response' => $response,
55
            'duration_ms' => \round((\microtime(true) - $start) * 1000),
56
        ]);
57
58
        return $response;
59
    }
60
}
61