Completed
Push — master ( 0f71cc...99d0b0 )
by Karel
15:42
created

ElasticaLogger::logQuery()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 18
cts 18
cp 1
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 17
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
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Logger;
13
14
use Psr\Log\AbstractLogger;
15
use Psr\Log\LoggerInterface;
16
17
/**
18
 * Logger for the Elastica.
19
 *
20
 * The {@link logQuery()} method is configured as the logger callable in the
21
 * service container.
22
 *
23
 * @author Gordon Franke <[email protected]>
24
 */
25
class ElasticaLogger extends AbstractLogger
26
{
27
    /**
28
     * @var LoggerInterface
29
     */
30
    protected $logger;
31
32
    /**
33
     * @var array
34
     */
35
    protected $queries = [];
36
37
    /**
38
     * @var bool
39
     */
40
    protected $debug;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param LoggerInterface|null $logger The Symfony logger
46
     * @param bool                 $debug
47
     */
48 27
    public function __construct(LoggerInterface $logger = null, $debug = false)
49
    {
50 27
        $this->logger = $logger;
51 27
        $this->debug = $debug;
52 27
    }
53
54
    /**
55
     * Logs a query.
56
     *
57
     * @param string $path       Path to call
58
     * @param string $method     Rest method to use (GET, POST, DELETE, PUT)
59
     * @param array  $data       Arguments
60
     * @param float  $queryTime  Execution time (in seconds)
61
     * @param array  $connection Host, port, transport, and headers of the query
62
     * @param array  $query      Arguments
63
     * @param int    $engineTime
64
     * @param int    $itemCount
65
     */
66 13
    public function logQuery($path, $method, $data, $queryTime, $connection = [], $query = [], $engineTime = 0, $itemCount = 0)
67
    {
68 13
        $executionMS = $queryTime * 1000;
69
70 13
        if ($this->debug) {
71 11
            $e = new \Exception();
72 11
            $this->queries[] = [
73 11
                'path' => $path,
74 11
                'method' => $method,
75 11
                'data' => $data,
76 11
                'executionMS' => $executionMS,
77 11
                'engineMS' => $engineTime,
78 11
                'connection' => $connection,
79 11
                'queryString' => $query,
80 11
                'itemCount' => $itemCount,
81 11
                'backtrace' => $e->getTraceAsString(),
82
            ];
83
        }
84
85 13
        if (null !== $this->logger) {
86 10
            $message = sprintf('%s (%s) %0.2f ms', $path, $method, $executionMS);
87 10
            $this->logger->info($message, (array) $data);
88
        }
89 13
    }
90
91
    /**
92
     * Returns the number of queries that have been logged.
93
     *
94
     * @return int The number of queries logged
95
     */
96 3
    public function getNbQueries()
97
    {
98 3
        return count($this->queries);
99
    }
100
101
    /**
102
     * Returns a human-readable array of queries logged.
103
     *
104
     * @return array An array of queries
105
     */
106 1
    public function getQueries()
107
    {
108 1
        return $this->queries;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 18
    public function log($level, $message, array $context = [])
115
    {
116 18
        return $this->logger->log($level, $message, $context);
117
    }
118
119
    public function reset()
120
    {
121
        $this->queries = [];
122
    }
123
}
124