Completed
Pull Request — master (#1413)
by
unknown
05:55
created

ElasticaLogger::logQuery()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 19
cts 19
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 6
nop 9
crap 4

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 28
    public function __construct(LoggerInterface $logger = null, $debug = false)
49
    {
50 28
        $this->logger = $logger;
51 28
        $this->debug = $debug;
52 28
    }
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
     * @param \Exception|null $exception
66
     */
67 14
    public function logQuery($path, $method, $data, $queryTime, $connection = [], $query = [], $engineTime = 0, $itemCount = 0, \Exception $exception = null)
68
    {
69 14
        $executionMS = $queryTime * 1000;
70
71 14
        if ($this->debug) {
72 12
            $e = new \Exception();
73 12
            $this->queries[] = [
74 12
                'path' => $path,
75 12
                'method' => $method,
76 12
                'data' => $data,
77 12
                'executionMS' => $executionMS,
78 12
                'engineMS' => $engineTime,
79 12
                'connection' => $connection,
80 12
                'queryString' => $query,
81 12
                'itemCount' => $itemCount,
82 12
                'backtrace' => $e->getTraceAsString(),
83 12
                'exceptionMessage' => $exception ? $exception->getMessage() : null,
84
            ];
85
        }
86
87 14
        if (null !== $this->logger) {
88 11
            $message = sprintf('%s (%s) %0.2f ms', $path, $method, $executionMS);
89 11
            $this->logger->info($message, (array) $data);
90
        }
91 14
    }
92
93
    /**
94
     * Returns the number of queries that have been logged.
95
     *
96
     * @return int The number of queries logged
97
     */
98 4
    public function getNbQueries()
99
    {
100 4
        return count($this->queries);
101
    }
102
103
    /**
104
     * Returns a human-readable array of queries logged.
105
     *
106
     * @return array An array of queries
107
     */
108 2
    public function getQueries()
109
    {
110 2
        return $this->queries;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 18
    public function log($level, $message, array $context = [])
117
    {
118 18
        return $this->logger->log($level, $message, $context);
119
    }
120
121
    public function reset()
122
    {
123
        $this->queries = [];
124
    }
125
}
126