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; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Psr\Http\Message\RequestInterface; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use Psr\Log\LogLevel; |
18
|
|
|
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Exception\FormatterStopException; |
19
|
|
|
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Message\DefaultStopMessage; |
20
|
|
|
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class StopFormatter. |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
class StopFormatter implements RequestStopInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
use FormatterTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param callable|null $msg A callable used to create the message |
31
|
|
|
* @param string $logLevel The level this should be logged at |
32
|
|
|
* |
33
|
|
|
* @return \Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\StopFormatter |
34
|
|
|
*/ |
35
|
|
|
public static function create( |
36
|
|
|
callable $msg = null, |
37
|
|
|
$logLevel = LogLevel::DEBUG |
38
|
|
|
) { |
39
|
|
|
if (!$msg) { |
40
|
|
|
$msg = new DefaultStopMessage(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return new self($msg, $logLevel); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* StartFormatter constructor. |
48
|
|
|
* |
49
|
|
|
* @param callable $msg A callable to format the messages |
50
|
|
|
* @param callable|string $level The log level for when the timer ends |
51
|
|
|
*/ |
52
|
|
|
public function __construct(callable $msg, $level = LogLevel::DEBUG) |
53
|
|
|
{ |
54
|
|
|
$this->msg = $msg; |
55
|
|
|
$this->level = $level; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function stop( |
62
|
|
|
TimerInterface $timer, |
63
|
|
|
RequestInterface $request, |
64
|
|
|
ResponseInterface $response |
65
|
|
|
) { |
66
|
|
|
try { |
67
|
|
|
return $this->msg($timer, $request, $response); |
68
|
|
|
} catch (Exception $e) { |
69
|
|
|
$msg = 'Error attempting to parse for log'; |
70
|
|
|
throw new FormatterStopException( |
71
|
|
|
$msg, |
72
|
|
|
FormatterStopException::MESSAGE_PARSE_EXCEPTION, |
73
|
|
|
$e |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function levelStop( |
82
|
|
|
TimerInterface $timer, |
83
|
|
|
RequestInterface $request, |
84
|
|
|
ResponseInterface $response |
85
|
|
|
) { |
86
|
|
|
return $this->level($timer, $request, $response); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.