Completed
Push — master ( 6a9619...7736e4 )
by Tobias
20s
created

LoggerPlugin::doHandleRequest()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 30
cts 30
cp 1
rs 9.232
c 0
b 0
f 0
cc 2
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Http\Client\Common\Plugin;
4
5
use Http\Client\Common\Plugin;
6
use Http\Client\Exception;
7
use Http\Message\Formatter;
8
use Http\Message\Formatter\SimpleFormatter;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Log\LoggerInterface;
12
13
/**
14
 * Log request, response and exception for an HTTP Client.
15
 *
16
 * @author Joel Wurtz <[email protected]>
17
 */
18
final class LoggerPlugin implements Plugin
0 ignored issues
show
Bug introduced by
There is one abstract method handleRequest in this class; you could implement it, or declare this class as abstract.
Loading history...
19
{
20
    use VersionBridgePlugin;
21
22
    private $logger;
23
24 5
    private $formatter;
25
26 5
    public function __construct(LoggerInterface $logger, Formatter $formatter = null)
27 5
    {
28 5
        $this->logger = $logger;
29
        $this->formatter = $formatter ?: new SimpleFormatter();
30
    }
31
32
    protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
33 3
    {
34
        $start = microtime(true);
35 3
        $this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]);
36 3
37
        return $next($request)->then(function (ResponseInterface $response) use ($request, $start) {
38
            $milliseconds = (int) round((microtime(true) - $start) * 1000);
39 1
            $this->logger->info(
40 1
                sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
41 1
                [
42
                    'request' => $request,
43 1
                    'response' => $response,
44 1
                    'milliseconds' => $milliseconds,
45 1
                ]
46
            );
47 1
48
            return $response;
49 1
        }, function (Exception $exception) use ($request, $start) {
50 3
            $milliseconds = (int) round((microtime(true) - $start) * 1000);
51 2
            if ($exception instanceof Exception\HttpException) {
52 2
                $this->logger->error(
53 1
                    sprintf("Error:\n%s\nwith response:\n%s\n\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)),
54 1
                    [
55
                        'request' => $request,
56 1
                        'response' => $exception->getResponse(),
57 2
                        'exception' => $exception,
58 1
                        'milliseconds' => $milliseconds,
59 1
                    ]
60
                );
61 1
            } else {
62 1
                $this->logger->error(
63 1
                    sprintf("Error:\n%s\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatRequest($request)),
64 1
                    [
65
                        'request' => $request,
66 1
                        'exception' => $exception,
67 1
                        'milliseconds' => $milliseconds,
68 1
                    ]
69
                );
70 1
            }
71
72
            throw $exception;
73 2
        });
74 3
    }
75
}
76