StopFormatter::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Formatter\Traits\FormatterConstructorTrait;
21
use Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\Traits\FormatterTrait;
22
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface;
23
24
/**
25
 * Class StopFormatter.
26
 */
27
final class StopFormatter implements RequestStopInterface
28
{
29
    use FormatterTrait;
30
    use FormatterConstructorTrait;
31
32
    /**
33
     * @param callable|null $msg      A callable used to create the message
34
     * @param string        $logLevel The level this should be logged at
35
     *
36
     * @return \Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\StopFormatter
37
     */
38
    public static function create(
39
        callable $msg = null,
40
        $logLevel = LogLevel::DEBUG
41
    ) {
42
        if (!$msg) {
43
            $msg = new DefaultStopMessage();
44
        }
45
46
        return new static($msg, $logLevel);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function stop(
53
        TimerInterface $timer,
54
        RequestInterface $request,
55
        ResponseInterface $response
56
    ) {
57
        try {
58
            return $this->msg($timer, $request, $response);
59
        } catch (Exception $ex) {
60
            throw FormatterStopException::msg($ex);
61
        }
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function levelStop(
68
        TimerInterface $timer,
69
        RequestInterface $request,
70
        ResponseInterface $response
71
    ) {
72
        try {
73
            return $this->level($timer, $request, $response);
74
        } catch (Exception $ex) {
75
            throw FormatterStopException::level($ex);
76
        }
77
    }
78
}
79