Completed
Push — master ( 98dd85...467f0f )
by GBProd
02:08
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 23
    public function __construct(LoggerInterface $logger = null, $debug = false)
36
    {
37 23
        $this->logger = $logger;
38 23
        $this->debug  = $debug;
39 23
    }
40
41
    /**
42
     * Returns the number of queries that have been logged.
43
     *
44
     * @return integer The number of queries logged
45
     */
46 3
    public function getNbQueries()
47
    {
48 3
        return count($this->queries);
49
    }
50
51
    /**
52
     * Returns a human-readable array of queries logged.
53
     *
54
     * @return array An array of queries
55
     */
56 1
    public function getQueries()
57
    {
58 1
        return $this->queries;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 5
    public function debug($message, array $context = array())
65
    {
66 5
        if ($this->debug) {
67 2
            $this->queries[] = $context;
68 2
        }
69
70 5
        if (!$this->logger) {
71 4
            return;
72
        }
73
74 1
        return $this->logger->debug($message, $context);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 2
    public function emergency($message, array $context = array())
81
    {
82 2
        if (!$this->logger) {
83 1
            return;
84
        }
85
86 1
        return $this->logger->emergency($message, $context);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 2
    public function alert($message, array $context = array())
93
    {
94 2
        if (!$this->logger) {
95 1
            return;
96
        }
97
98 1
        return $this->logger->alert($message, $context);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 2
    public function critical($message, array $context = array())
105
    {
106 2
        if (!$this->logger) {
107 1
            return;
108
        }
109
110 1
        return $this->logger->critical($message, $context);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 2
    public function error($message, array $context = array())
117
    {
118 2
        if (!$this->logger) {
119 1
            return;
120
        }
121
122 1
        return $this->logger->error($message, $context);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 2
    public function warning($message, array $context = array())
129
    {
130 2
        if (!$this->logger) {
131 1
            return;
132
        }
133
134 1
        return $this->logger->warning($message, $context);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 2
    public function notice($message, array $context = array())
141
    {
142 2
        if (!$this->logger) {
143 1
            return;
144
        }
145
146 1
        return $this->logger->notice($message, $context);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 2
    public function info($message, array $context = array())
153
    {
154 2
        if (!$this->logger) {
155 1
            return;
156
        }
157
158 1
        return $this->logger->info($message, $context);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 2
    public function log($level, $message, array $context = array())
165
    {
166 2
        if (!$this->logger) {
167 1
            return;
168
        }
169
170 1
        return $this->logger->log($level, $message, $context);
171
    }
172
}
173