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\Formatter\Traits; |
13
|
|
|
|
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait FormatterTrait. |
20
|
|
|
*/ |
21
|
|
|
trait FormatterTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param TimerInterface $timer A Timer to format |
25
|
|
|
* @param RequestInterface $request A Request to format |
26
|
|
|
* @param ResponseInterface|null $response The Response to format |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
private function msg( |
31
|
|
|
TimerInterface $timer, |
32
|
|
|
RequestInterface $request, |
33
|
|
|
ResponseInterface $response = null |
34
|
|
|
) { |
35
|
|
|
$msg = $this->getMsg(); |
36
|
|
|
|
37
|
|
|
return (string) $msg($timer, $request, $response); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param \Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface $timer A Timer to format |
42
|
|
|
* @param \Psr\Http\Message\RequestInterface $request A Request to format |
43
|
|
|
* @param \Psr\Http\Message\ResponseInterface|null $response The Response to format |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
private function level( |
48
|
|
|
TimerInterface $timer, |
49
|
|
|
RequestInterface $request, |
50
|
|
|
ResponseInterface $response = null |
51
|
|
|
) { |
52
|
|
|
$level = $this->getLevel(); |
53
|
|
|
|
54
|
|
|
if (is_callable($level)) { |
55
|
|
|
$level = $level($timer, $request, $response); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return (string) $level; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return callable |
63
|
|
|
*/ |
64
|
|
|
abstract protected function getMsg(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string|callable |
68
|
|
|
*/ |
69
|
|
|
abstract protected function getLevel(); |
70
|
|
|
} |
71
|
|
|
|