LoggerPlugin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 66
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A handleRequest() 0 37 2
1
<?php
2
3
namespace Http\Client\Plugin;
4
5 1
@trigger_error('The '.__NAMESPACE__.'\LoggerPlugin class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\Plugin\LoggerPlugin instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
6
7
use Http\Client\Exception;
8
use Http\Message\Formatter;
9
use Http\Message\Formatter\SimpleFormatter;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Log\LoggerInterface;
13
14
/**
15
 * Log request, response and exception for a HTTP Client.
16
 *
17
 * @author Joel Wurtz <[email protected]>
18
 *
19
 * @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\LoggerPlugin} instead.
20
 */
21
class LoggerPlugin implements Plugin
0 ignored issues
show
Deprecated Code introduced by
The interface Http\Client\Plugin\Plugin has been deprecated with message: since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
22
{
23
    /**
24
     * Logger to log request / response / exception for a http call.
25
     *
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
30
    /**
31
     * Formats a request/response as string.
32
     *
33
     * @var Formatter
34
     */
35
    private $formatter;
36
37
    /**
38
     * @param LoggerInterface $logger
39
     */
40 5
    public function __construct(LoggerInterface $logger, Formatter $formatter = null)
41
    {
42 5
        $this->logger = $logger;
43 5
        $this->formatter = $formatter ?: new SimpleFormatter();
44 5
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 3
    public function handleRequest(RequestInterface $request, callable $next, callable $first)
50
    {
51 3
        $this->logger->info(sprintf('Emit request: "%s"', $this->formatter->formatRequest($request)), ['request' => $request]);
52
53
        return $next($request)->then(function (ResponseInterface $response) use ($request) {
54 1
            $this->logger->info(
55 1
                sprintf('Receive response: "%s" for request: "%s"', $this->formatter->formatResponse($response), $this->formatter->formatRequest($request)),
56
                [
57 1
                    'request' => $request,
58 1
                    'response' => $response,
59
                ]
60 1
            );
61
62 1
            return $response;
63 3
        }, function (Exception $exception) use ($request) {
64 2
            if ($exception instanceof Exception\HttpException) {
65 1
                $this->logger->error(
66 1
                    sprintf('Error: "%s" with response: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)),
67
                    [
68 1
                        'request' => $request,
69 1
                        'response' => $exception->getResponse(),
70 1
                        'exception' => $exception,
71
                    ]
72 1
                );
73 1
            } else {
74 1
                $this->logger->error(
75 1
                    sprintf('Error: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatRequest($request)),
76
                    [
77 1
                        'request' => $request,
78 1
                        'exception' => $exception,
79
                    ]
80 1
                );
81
            }
82
83 2
            throw $exception;
84 3
        });
85
    }
86
}
87