ApiLogger   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 20
dl 0
loc 54
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getCallsCount() 0 3 1
A logCall() 0 19 3
A getCalls() 0 3 1
1
<?php
2
3
namespace Nixilla\Api\LoggerBundle\Logger;
4
use Psr\Log\LoggerInterface;
5
use Psr\Log\NullLogger;
6
7
class ApiLogger implements ApiInterface
8
{
9
    /**
10
     * @var LoggerInterface
11
     */
12
    protected $logger;
13
14
    /**
15
     * @var array
16
     */
17
    protected $calls = [];
18
19
    /**
20
     * @var bool
21
     */
22
    protected $debug = false;
23
24
25 2
    public function __construct(LoggerInterface $logger = null, $debug = false)
26
    {
27 2
        $this->logger = $logger ?: new NullLogger();
28 2
        $this->debug = $debug;
29 2
    }
30
31 1
    public function logCall($host, $path, $method, $time, array $requestHeaders = [], array $params = [], array $responseHeaders = [], $result = null)
32
    {
33 1
        if($this->debug)
34
        {
35 1
            $this->calls[] = [
36 1
                'host' => $host,
37 1
                'path' => $path,
38 1
                'method' => $method,
39 1
                'time' => $time,
40 1
                'request_headers' => $requestHeaders,
41 1
                'params' => $params,
42 1
                'response_headers' => $responseHeaders,
43 1
                'result' => $result
44
            ];
45
        }
46
47 1
        if(null !== $this->logger)
48
        {
49 1
            $this->logger->info(sprintf("%s (%s) %0.2f ms, params: %s", $path, $method, $time * 1000, json_encode($params)));
50
        }
51 1
    }
52
53 1
    public function getCalls()
54
    {
55 1
        return $this->calls;
56
    }
57
58 1
    public function getCallsCount()
59
    {
60 1
        return count($this->calls);
61
    }
62
}