StartFormatter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 9 2
A start() 0 6 2
A levelStart() 0 6 2
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\Formatter\Traits\FormatterConstructorTrait;
20
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Traits\FormatterTrait;
21
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface;
22
23
/**
24
 * Class StartFormatter.
25
 */
26
final class StartFormatter implements RequestStartInterface
27
{
28
    use FormatterTrait;
29
    use FormatterConstructorTrait;
30
31
    /**
32
     * @param callable|null $msg      A callable used to create the message
33
     * @param string        $logLevel The level this should be logged at
34
     *
35
     * @return \Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\StartFormatter
36
     */
37
    public static function create(
38
        callable $msg = null,
39
        $logLevel = LogLevel::DEBUG
40
    ) {
41
        if (!$msg) {
42
            $msg = new DefaultStartMessage();
43
        }
44
45
        return new static($msg, $logLevel);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function start(TimerInterface $timer, RequestInterface $request)
52
    {
53
        try {
54
            return $this->msg($timer, $request);
55
        } catch (Exception $e) {
56
            throw FormatterStartException::msg($e);
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function levelStart(TimerInterface $timer, RequestInterface $request)
64
    {
65
        try {
66
            return $this->level($timer, $request);
67
        } catch (Exception $e) {
68
            throw FormatterStartException::level($e);
69
        }
70
    }
71
}
72