Completed
Push — master ( 73ec26...13e542 )
by Pol
02:41
created

LoggerPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 5

Test Coverage

Coverage 46.15%

Importance

Changes 0
Metric Value
wmc 4
cbo 5
dl 0
loc 63
ccs 12
cts 26
cp 0.4615
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
B handleRequest() 0 32 2
1
<?php
2
3
namespace drupol\Yaroc\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
 * Logger plugin for YAROC.
15
 */
16
class LoggerPlugin implements Plugin {
17
  /**
18
   * Logger to log request / response / exception for a http call.
19
   *
20
   * @var LoggerInterface
21
   */
22
  private $logger;
23
24
  /**
25
   * Formats a request/response as string.
26
   *
27
   * @var Formatter
28
   */
29
  private $formatter;
30
31
  /**
32
   * LoggerPlugin constructor.
33
   *
34
   * @param \Psr\Log\LoggerInterface $logger
35
   * @param \Http\Message\Formatter|NULL $formatter
36
   */
37 15
  public function __construct(LoggerInterface $logger, Formatter $formatter = NULL) {
38 15
    $this->logger = $logger;
39 15
    $this->formatter = $formatter ?: new SimpleFormatter();
40 15
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45 12
  public function handleRequest(RequestInterface $request, callable $next, callable $first) {
46 12
    $this->logger->info(sprintf('Emit request: "%s"', $this->formatter->formatRequest($request)));
47
48
    return $next($request)->then(function (ResponseInterface $response) use ($request) {
49 12
      $this->logger->info(
50 12
        sprintf('Receive response: "%s" for request: "%s"', $this->formatter->formatResponse($response), $this->formatter->formatRequest($request))
51 12
      );
52
53 12
      return $response;
54 12
    }, function (Exception $exception) use ($request) {
55
      if ($exception instanceof Exception\HttpException) {
56
        $this->logger->error(
57
          sprintf('Error: "%s" with response: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatResponse($exception->getResponse()), $this->formatter->formatRequest($request)),
58
          [
59
            'request' => $request,
60
            'response' => $exception->getResponse(),
61
            'exception' => $exception,
62
          ]
63
        );
64
      } else {
65
        $this->logger->error(
66
          sprintf('Error: "%s" when emitting request: "%s"', $exception->getMessage(), $this->formatter->formatRequest($request)),
67
          [
68
            'request' => $request,
69
            'exception' => $exception,
70
          ]
71
        );
72
      }
73
74
      throw $exception;
75 12
    });
76
  }
77
78
}
79