ApiLogger::logCall()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.8666
cc 3
nc 4
nop 8
crap 3

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
}