Completed
Pull Request — master (#11)
by Barney
03:39
created

StopFormatter::levelStop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 1
nc 1
nop 3
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\Timer\TimerInterface;
21
22
/**
23
 * Class StopFormatter.
24
 */
25 View Code Duplication
class StopFormatter implements RequestStopInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
{
27
    use FormatterTrait;
28
29
    /**
30
     * @param callable|null $msg      A callable used to create the message
31
     * @param string        $logLevel The level this should be logged at
32
     *
33
     * @return \Shrikeh\GuzzleMiddleware\TimerLogger\Formatter\StopFormatter
34
     */
35
    public static function create(
36
        callable $msg = null,
37
        $logLevel = LogLevel::DEBUG
38
    ) {
39
        if (!$msg) {
40
            $msg = new DefaultStopMessage();
41
        }
42
43
        return new self($msg, $logLevel);
44
    }
45
46
    /**
47
     * StartFormatter constructor.
48
     *
49
     * @param callable        $msg   A callable to format the messages
50
     * @param callable|string $level The log level for when the timer ends
51
     */
52
    public function __construct(callable $msg, $level = LogLevel::DEBUG)
53
    {
54
        $this->msg = $msg;
55
        $this->level = $level;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function stop(
62
        TimerInterface $timer,
63
        RequestInterface $request,
64
        ResponseInterface $response
65
    ) {
66
        try {
67
            return $this->msg($timer, $request, $response);
68
        } catch (Exception $e) {
69
            $msg = 'Error attempting to parse for log';
70
            throw new FormatterStopException(
71
                $msg,
72
                FormatterStopException::MESSAGE_PARSE_EXCEPTION,
73
                $e
74
            );
75
        }
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function levelStop(
82
        TimerInterface $timer,
83
        RequestInterface $request,
84
        ResponseInterface $response
85
    ) {
86
        return $this->level($timer, $request, $response);
87
    }
88
}
89