Completed
Pull Request — master (#10)
by GBProd
09:10
created

ElasticaLogger::logQuery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 20
cts 20
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 8
crap 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ElasticaLogger::getQueries() 0 4 1
A ElasticaLogger::debug() 0 12 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 GBProd\ElasticaBundle\Logger;
4
5
use Psr\Log\LoggerInterface;
6
7
/**
8
 * Logger for Elastica
9
 *
10
 * @author GBProd <[email protected]>
11
 */
12
class ElasticaLogger implements LoggerInterface
13
{
14
    /**
15
     * @var LoggerInterface
16
     */
17
    protected $logger;
18
19
    /**
20
     * @var array
21
     */
22
    protected $queries = array();
23
24
    /**
25
     * @var boolean
26
     */
27
    protected $debug;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param LoggerInterface|null $logger The Symfony logger
33
     * @param boolean              $debug
34
     */
35
    public function __construct(LoggerInterface $logger = null, $debug = false)
36
    {
37
        $this->logger = $logger;
38 24
        $this->debug  = $debug;
39
    }
40 24
41 24
    /**
42 24
     * Returns the number of queries that have been logged.
43
     *
44
     * @return integer The number of queries logged
45
     */
46
    public function getNbQueries()
47
    {
48
        return count($this->queries);
49
    }
50
51
    /**
52
     * Returns a human-readable array of queries logged.
53
     *
54 4
     * @return array An array of queries
55
     */
56 4
    public function getQueries()
57 2
    {
58
        return $this->queries;
59 2
    }
60 2
61 2
    /**
62 2
     * {@inheritdoc}
63 2
     */
64 2
    public function debug($message, array $context = array())
65 2
    {
66 2
        if ($this->debug) {
67 2
            $this->queries[] = $context;
68 2
        }
69 2
70 2
        if (!$this->logger) {
71
            return;
72 4
        }
73 1
74 1
        return $this->logger->debug($message, $context);
75 1
    }
76 4
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function emergency($message, array $context = array())
81
    {
82
        if (!$this->logger) {
83 3
            return;
84
        }
85 3
86
        return $this->logger->emergency($message, $context);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function alert($message, array $context = array())
93 1
    {
94
        if (!$this->logger) {
95 1
            return;
96
        }
97
98
        return $this->logger->alert($message, $context);
99
    }
100
101 2
    /**
102
     * {@inheritdoc}
103 2
     */
104 1
    public function critical($message, array $context = array())
105
    {
106
        if (!$this->logger) {
107 1
            return;
108
        }
109
110
        return $this->logger->critical($message, $context);
111
    }
112
113 2
    /**
114
     * {@inheritdoc}
115 2
     */
116 1
    public function error($message, array $context = array())
117
    {
118
        if (!$this->logger) {
119 1
            return;
120
        }
121
122
        return $this->logger->error($message, $context);
123
    }
124
125 2
    /**
126
     * {@inheritdoc}
127 2
     */
128 1
    public function warning($message, array $context = array())
129
    {
130
        if (!$this->logger) {
131 1
            return;
132
        }
133
134
        return $this->logger->warning($message, $context);
135
    }
136
137 2
    /**
138
     * {@inheritdoc}
139 2
     */
140 1
    public function notice($message, array $context = array())
141
    {
142
        if (!$this->logger) {
143 1
            return;
144
        }
145
146
        return $this->logger->notice($message, $context);
147
    }
148
149 2
    /**
150
     * {@inheritdoc}
151 2
     */
152 1
    public function info($message, array $context = array())
153
    {
154
        if (!$this->logger) {
155 1
            return;
156
        }
157
158
        return $this->logger->info($message, $context);
159
    }
160
161 2
    /**
162
     * {@inheritdoc}
163 2
     */
164 1
    public function log($level, $message, array $context = array())
165
    {
166
        if (!$this->logger) {
167 1
            return;
168
        }
169
170
        return $this->logger->log($level, $message, $context);
171
    }
172
}
173