FormatterTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A msg() 0 8 1
A level() 0 12 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\Traits;
13
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface;
17
18
/**
19
 * Trait FormatterTrait.
20
 */
21
trait FormatterTrait
22
{
23
    /**
24
     * @param TimerInterface         $timer    A Timer to format
25
     * @param RequestInterface       $request  A Request to format
26
     * @param ResponseInterface|null $response The Response to format
27
     *
28
     * @return string
29
     */
30
    private function msg(
31
        TimerInterface $timer,
32
        RequestInterface $request,
33
        ResponseInterface $response = null
34
    ) {
35
        $msg = $this->getMsg();
36
37
        return (string) $msg($timer, $request, $response);
38
    }
39
40
    /**
41
     * @param \Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface $timer    A Timer to format
42
     * @param \Psr\Http\Message\RequestInterface                         $request  A Request to format
43
     * @param \Psr\Http\Message\ResponseInterface|null                   $response The Response to format
44
     *
45
     * @return string
46
     */
47
    private function level(
48
        TimerInterface $timer,
49
        RequestInterface $request,
50
        ResponseInterface $response = null
51
    ) {
52
        $level = $this->getLevel();
53
54
        if (is_callable($level)) {
55
            $level = $level($timer, $request, $response);
56
        }
57
58
        return (string) $level;
59
    }
60
61
    /**
62
     * @return callable
63
     */
64
    abstract protected function getMsg();
65
66
    /**
67
     * @return string|callable
68
     */
69
    abstract protected function getLevel();
70
}
71