Completed
Pull Request — master (#13)
by Barney
02:19
created

StartFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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
namespace Shrikeh\GuzzleMiddleware\TimerLogger\Formatter;
12
13
use Exception;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Log\LogLevel;
16
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Exception\FormatterStartException;
17
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Message\DefaultStartMessage;
18
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Traits\FormatterConstructorTrait;
19
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Traits\FormatterTrait;
20
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface;
21
22
/**
23
 * Class StartFormatter.
24
 */
25
final class StartFormatter implements RequestStartInterface
26
{
27
    use FormatterTrait;
28
    use FormatterConstructorTrait;
29
30
    /**
31
     * @param callable|null $msg      A callable used to create the message
32
     * @param string        $logLevel The level this should be logged at
33
     *
34
     * @return \Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\StartFormatter
35
     */
36
    public static function create(
37
        callable $msg = null,
38
        $logLevel = LogLevel::DEBUG
39
    ) {
40
        if (!$msg) {
41
            $msg = new DefaultStartMessage();
42
        }
43
44
        return new self($msg, $logLevel);
45
    }
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function start(TimerInterface $timer, RequestInterface $request)
50
    {
51
        try {
52
            return $this->msg($timer, $request);
53
        } catch (Exception $e) {
54
            throw new FormatterStartException(
55
                FormatterStartException::MESSAGE_START_MSG,
56
                FormatterStartException::MESSAGE_PARSE_CODE,
57
                $e
58
            );
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function levelStart(TimerInterface $timer, RequestInterface $request)
66
    {
67
        return $this->level($timer, $request);
68
    }
69
}
70