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

ElasticaLogger   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.92%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 0
loc 166
ccs 47
cts 48
cp 0.9792
rs 10
c 0
b 0
f 0

12 Methods

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