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

StopFormatter::__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
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
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 self($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 $e) {
60
            $msg = 'Error attempting to parse for log';
61
            throw new FormatterStopException(
62
                $msg,
63
                FormatterStopException::MESSAGE_PARSE_EXCEPTION,
64
                $e
65
            );
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function levelStop(
73
        TimerInterface $timer,
74
        RequestInterface $request,
75
        ResponseInterface $response
76
    ) {
77
        return $this->level($timer, $request, $response);
78
    }
79
}
80