Completed
Pull Request — master (#15)
by Barney
02:52
created

StartFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 9 2
A start() 0 9 2
A levelStart() 0 3 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\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 self($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 new FormatterStartException(
57
                FormatterStartException::MESSAGE_START_MSG,
58
                FormatterStartException::MESSAGE_PARSE_CODE,
59
                $e
60
            );
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function levelStart(TimerInterface $timer, RequestInterface $request)
68
    {
69
        return $this->level($timer, $request);
70
    }
71
}
72