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\Log\LogLevel; |
17
|
|
|
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Exception\FormatterStartException; |
18
|
|
|
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Message\DefaultStartMessage; |
19
|
|
|
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class StartFormatter. |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
class StartFormatter implements RequestStartInterface |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
use FormatterTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param callable|null $msg A callable used to create the message |
30
|
|
|
* @param string $logLevel The level this should be logged at |
31
|
|
|
* |
32
|
|
|
* @return \Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\StartFormatter |
33
|
|
|
*/ |
34
|
|
|
public static function create( |
35
|
|
|
callable $msg = null, |
36
|
|
|
$logLevel = LogLevel::DEBUG |
37
|
|
|
) { |
38
|
|
|
if (!$msg) { |
39
|
|
|
$msg = new DefaultStartMessage(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return new self($msg, $logLevel); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* StartFormatter constructor. |
47
|
|
|
* |
48
|
|
|
* @param callable $msg A callable used to create the message |
49
|
|
|
* @param callable|string $level The level this should be logged at |
50
|
|
|
*/ |
51
|
|
|
private function __construct(callable $msg, $level = LogLevel::DEBUG) |
52
|
|
|
{ |
53
|
|
|
$this->msg = $msg; |
54
|
|
|
$this->level = $level; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function start(TimerInterface $timer, RequestInterface $request) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
return $this->msg($timer, $request); |
64
|
|
|
} catch (Exception $e) { |
65
|
|
|
$msg = 'Error attempting to parse for log'; |
66
|
|
|
throw new FormatterStartException( |
67
|
|
|
$msg, |
68
|
|
|
FormatterStartException::MESSAGE_PARSE_EXCEPTION, |
69
|
|
|
$e |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function levelStart(TimerInterface $timer, RequestInterface $request) |
78
|
|
|
{ |
79
|
|
|
return $this->level($timer, $request); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.