Completed
Pull Request — develop (#22)
by Barney
01:41
created

ResponseTimeLogger::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\ResponseTimeLogger;
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\Formatter\Verbose;
20
use Shrikeh\GuzzleMiddleware\TimerLogger\RequestTimers\RequestTimers;
21
use Shrikeh\GuzzleMiddleware\TimerLogger\RequestTimers\RequestTimersInterface;
22
use Shrikeh\GuzzleMiddleware\TimerLogger\ResponseLogger\ResponseLogger;
23
use Shrikeh\GuzzleMiddleware\TimerLogger\ResponseLogger\ResponseLoggerInterface;
24
use Shrikeh\GuzzleMiddleware\TimerLogger\ResponseTimeLogger\Exception\TimersException;
25
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface;
26
27
/**
28
 * Class ResponseTimeLogger.
29
 */
30
final class ResponseTimeLogger implements ResponseTimeLoggerInterface
31
{
32
    /**
33
     * @var RequestTimersInterface
34
     */
35
    private $timers;
36
37
    /**
38
     * @var \Shrikeh\GuzzleMiddleware\TimerLogger\ResponseLogger\ResponseLoggerInterface
39
     */
40
    private $logger;
41
42
    /**
43
     * @param \Psr\Log\LoggerInterface $logger    A logger to log to
44
     * @param FormatterInterface       $formatter An optional formatter
45
     *
46
     * @return ResponseTimeLogger
47
     */
48
    public static function quickStart(
49
        LoggerInterface $logger,
50
        FormatterInterface $formatter = null
51
    ) {
52
        if (!$formatter) {
53
            $formatter = Verbose::quickStart();
54
        }
55
56
        return self::createFrom(new ResponseLogger($logger, $formatter));
57
    }
58
59
    /**
60
     * @param ResponseLoggerInterface     $logger a logger to log to
61
     * @param RequestTimersInterface|null $timers An optional timers collection
62
     *
63
     * @return ResponseTimeLogger
64
     */
65
    public static function createFrom(
66
        ResponseLoggerInterface $logger,
67
        RequestTimersInterface $timers = null
68
    ) {
69
        if (!$timers) {
70
            $timers = new RequestTimers();
71
        }
72
73
        return new self($timers, $logger);
74
    }
75
76
    /**
77
     * @param RequestTimersInterface  $timers A timers collection
78
     * @param ResponseLoggerInterface $logger a logger to log to
79
     */
80
    public function __construct(
81
        RequestTimersInterface $timers,
82
        ResponseLoggerInterface $logger
83
    ) {
84
        $this->timers = $timers;
85
        $this->logger = $logger;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function start(RequestInterface $request)
92
    {
93
        $this->logger->logStart(
94
            $this->startTimer($request),
95
            $request
96
        );
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function stop(RequestInterface $request, ResponseInterface $response)
103
    {
104
        $this->logger->logStop(
105
            $this->stopTimer($request),
106
            $request,
107
            $response
108
        );
109
    }
110
111
    /**
112
     * @param \Psr\Http\Message\RequestInterface $request The Request
113
     *
114
     * @return TimerInterface
115
     */
116
    private function startTimer(RequestInterface $request)
117
    {
118
        try {
119
            return $this->timers->start($request);
120
        } catch (Exception $e) {
121
            throw TimersException::createFrom($e);
0 ignored issues
show
Bug introduced by
The method createFrom() does not exist on Shrikeh\GuzzleMiddleware...ception\TimersException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
            throw TimersException::/** @scrutinizer ignore-call */ createFrom($e);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
        }
123
    }
124
125
    /**
126
     * @param \Psr\Http\Message\RequestInterface $request The Request
127
     *
128
     * @return TimerInterface
129
     */
130
    private function stopTimer(RequestInterface $request)
131
    {
132
        try {
133
            return $this->timers->stop($request);
134
        } catch (Exception $e) {
135
            throw TimersException::createFrom($e);
136
        }
137
    }
138
}
139