Completed
Pull Request — master (#10)
by GBProd
01:48
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

1 Method

Rating   Name   Duplication   Size   Complexity  
A ElasticaLogger::getQueries() 0 4 1

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
            $e = new \Exception();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $e. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Unused Code introduced by
$e is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
69 2
            $this->queries[] = $context;
70 2
        }
71
72 5
        if (!$this->logger) {
73 4
            return;
74
        }
75
76 1
        return $this->logger->debug($message, $context);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 2
    public function emergency($message, array $context = array())
83
    {
84 2
        if (!$this->logger) {
85 1
            return;
86
        }
87
88 1
        return $this->logger->emergency($message, $context);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 2
    public function alert($message, array $context = array())
95
    {
96 2
        if (!$this->logger) {
97 1
            return;
98
        }
99
100 1
        return $this->logger->alert($message, $context);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 2
    public function critical($message, array $context = array())
107
    {
108 2
        if (!$this->logger) {
109 1
            return;
110
        }
111
112 1
        return $this->logger->critical($message, $context);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 2
    public function error($message, array $context = array())
119
    {
120 2
        if (!$this->logger) {
121 1
            return;
122
        }
123
124 1
        return $this->logger->error($message, $context);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 2
    public function warning($message, array $context = array())
131
    {
132 2
        if (!$this->logger) {
133 1
            return;
134
        }
135
136 1
        return $this->logger->warning($message, $context);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 2
    public function notice($message, array $context = array())
143
    {
144 2
        if (!$this->logger) {
145 1
            return;
146
        }
147
148 1
        return $this->logger->notice($message, $context);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 2
    public function info($message, array $context = array())
155
    {
156 2
        if (!$this->logger) {
157 1
            return;
158
        }
159
160 1
        return $this->logger->info($message, $context);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 2
    public function log($level, $message, array $context = array())
167
    {
168 2
        if (!$this->logger) {
169 1
            return;
170
        }
171
172 1
        return $this->logger->log($level, $message, $context);
173
    }
174
}
175