Completed
Pull Request — master (#9)
by Tobias
06:38
created

LoggerPlugin::handleRequest()   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
cc 2
nc 1
nop 3
dl 0
loc 43
ccs 30
cts 30
cp 1
crap 2
rs 9.232
c 0
b 0
f 0
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 extends VersionBridgePlugin
19
{
20
    private $logger;
21
22
    private $formatter;
23
24 5
    public function __construct(LoggerInterface $logger, Formatter $formatter = null)
25
    {
26 5
        $this->logger = $logger;
27 5
        $this->formatter = $formatter ?: new SimpleFormatter();
28 5
    }
29
30
    protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
31
    {
32
        $start = microtime(true);
33 3
        $this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['request' => $request]);
34
35 3
        return $next($request)->then(function (ResponseInterface $response) use ($request, $start) {
36 3
            $milliseconds = (int) round((microtime(true) - $start) * 1000);
37
            $this->logger->info(
38
                sprintf("Received response:\n%s\n\nfor request:\n%s", $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
39 1
                [
40 1
                    'request' => $request,
41 1
                    'response' => $response,
42
                    'milliseconds' => $milliseconds,
43 1
                ]
44 1
            );
45 1
46
            return $response;
47 1
        }, function (Exception $exception) use ($request, $start) {
48
            $milliseconds = (int) round((microtime(true) - $start) * 1000);
49 1
            if ($exception instanceof Exception\HttpException) {
50 3
                $this->logger->error(
51 2
                    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)),
52 2
                    [
53 1
                        'request' => $request,
54 1
                        'response' => $exception->getResponse(),
55
                        'exception' => $exception,
56 1
                        'milliseconds' => $milliseconds,
57 2
                    ]
58 1
                );
59 1
            } else {
60
                $this->logger->error(
61 1
                    sprintf("Error:\n%s\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatRequest($request)),
62 1
                    [
63 1
                        'request' => $request,
64 1
                        'exception' => $exception,
65
                        'milliseconds' => $milliseconds,
66 1
                    ]
67 1
                );
68 1
            }
69
70 1
            throw $exception;
71
        });
72
    }
73
}
74