Completed
Push — develop ( cc8c81...372a4c )
by Barney
14s
created

ResponseLogger::writeStop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * @codingStandardsIgnoreStart
4
 *
5
 * @author       Barney Hanlon <[email protected]>
6
 * @copyright    Barney Hanlon 2017
7
 * @license      https://opensource.org/licenses/MIT
8
 *
9
 * @codingStandardsIgnoreEnd
10
 */
11
12
namespace Shrikeh\GuzzleMiddleware\TimerLogger\ResponseLogger;
13
14
use Exception;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Log\LoggerInterface;
18
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\FormatterInterface;
19
use Shrikeh\GuzzleMiddleware\TimerLogger\ResponseLogger\Exception\ResponseLogStartException;
20
use Shrikeh\GuzzleMiddleware\TimerLogger\ResponseLogger\Exception\ResponseLogStopException;
21
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface;
22
23
/**
24
 * Class ResponseLogger.
25
 */
26
final class ResponseLogger implements ResponseLoggerInterface
27
{
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * @var FormatterInterface
35
     */
36
    private $formatter;
37
38
    /**
39
     * ResponseLogger constructor.
40
     *
41
     * @param LoggerInterface    $logger    The PSR-3 logger
42
     * @param FormatterInterface $formatter A formatter
43
     */
44
    public function __construct(
45
        LoggerInterface $logger,
46
        FormatterInterface $formatter
47
    ) {
48
        $this->logger = $logger;
49
        $this->formatter = $formatter;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function logStart(TimerInterface $timer, RequestInterface $request)
56
    {
57
        try {
58
            $this->writeStart($timer, $request);
59
60
            return $this;
61
        } catch (Exception $e) {
62
            throw ResponseLogStartException::start($e);
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function logStop(
70
        TimerInterface $timer,
71
        RequestInterface $request,
72
        ResponseInterface $response
73
    ) {
74
        try {
75
            $this->writeStop($timer, $request, $response);
76
77
            return $this;
78
        } catch (Exception $e) {
79
            throw ResponseLogStopException::stop($e);
80
        }
81
    }
82
83
    /**
84
     * @param TimerInterface                     $timer   The timer to log
85
     * @param \Psr\Http\Message\RequestInterface $request The Request to log
86
     */
87
    private function writeStart(TimerInterface $timer, RequestInterface $request)
88
    {
89
        $this->logger->log(
90
            $this->formatter->levelStart($timer, $request),
91
            $this->formatter->start($timer, $request)
92
        );
93
    }
94
95
    /**
96
     * @param TimerInterface                      $timer    The timer to log
97
     * @param \Psr\Http\Message\RequestInterface  $request  The Request to log against
98
     * @param \Psr\Http\Message\ResponseInterface $response The Response to log
99
     */
100
    private function writeStop(
101
        TimerInterface $timer,
102
        RequestInterface $request,
103
        ResponseInterface $response
104
    ) {
105
        $this->logger->log(
106
            $this->formatter->levelStop($timer, $request, $response),
107
            $this->formatter->stop($timer, $request, $response)
108
        );
109
    }
110
}
111