Completed
Push — 3.1.x ( b8b6d2...fd2df9 )
by Karel
14:12 queued 11:28
created

Client::logQuery()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092
Metric Value
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
rs 9.2
cc 4
eloc 11
nc 3
nop 5
crap 4.0092
1
<?php
2
3
namespace FOS\ElasticaBundle\Elastica;
4
5
use Elastica\Client as BaseClient;
6
use Elastica\Request;
7
use FOS\ElasticaBundle\Logger\ElasticaLogger;
8
use Symfony\Component\Stopwatch\Stopwatch;
9
10
/**
11
 * Extends the default Elastica client to provide logging for errors that occur
12
 * during communication with ElasticSearch.
13
 *
14
 * @author Gordon Franke <[email protected]>
15
 */
16
class Client extends BaseClient
17
{
18
    /**
19
     * Stores created indexes to avoid recreation.
20
     *
21
     * @var array
22
     */
23
    private $indexCache = array();
24
25
    /**
26
     * Symfony's debugging Stopwatch.
27
     *
28
     * @var Stopwatch|null
29
     */
30
    private $stopwatch;
31
32
    /**
33
     * @param string $path
34
     * @param string $method
35
     * @param array  $data
36
     * @param array  $query
37
     *
38
     * @return \Elastica\Response
39
     */
40 5
    public function request($path, $method = Request::GET, $data = array(), array $query = array())
41
    {
42 5
        if ($this->stopwatch) {
43 4
            $this->stopwatch->start('es_request', 'fos_elastica');
44 4
        }
45
46 5
        $start = microtime(true);
47 5
        $response = parent::request($path, $method, $data, $query);
48
49 5
        $this->logQuery($path, $method, $data, $query, $start);
50
51 5
        if ($this->stopwatch) {
52 4
            $this->stopwatch->stop('es_request');
53 4
        }
54
55 5
        return $response;
56
    }
57
58 5
    public function getIndex($name)
59
    {
60 5
        if (isset($this->indexCache[$name])) {
61
            return $this->indexCache[$name];
62
        }
63
64 5
        return $this->indexCache[$name] = new Index($this, $name);
65
    }
66
67
    /**
68
     * Sets a stopwatch instance for debugging purposes.
69
     *
70
     * @param Stopwatch $stopwatch
71
     */
72 6
    public function setStopwatch(Stopwatch $stopwatch = null)
73
    {
74 6
        $this->stopwatch = $stopwatch;
75 6
    }
76
77
    /**
78
     * Log the query if we have an instance of ElasticaLogger.
79
     *
80
     * @param string $path
81
     * @param string $method
82
     * @param array  $data
83
     * @param array  $query
84
     * @param int    $start
85
     */
86 5
    private function logQuery($path, $method, $data, array $query, $start)
87
    {
88 5
        if (!$this->_logger or !$this->_logger instanceof ElasticaLogger) {
89
            return;
90
        }
91
92 5
        $time = microtime(true) - $start;
93 5
        $connection = $this->getLastRequest()->getConnection();
94
95
        $connection_array = array(
96 5
            'host' => $connection->getHost(),
97 5
            'port' => $connection->getPort(),
98 5
            'transport' => $connection->getTransport(),
99 5
            'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : array(),
100 5
        );
101
102 5
        $this->_logger->logQuery($path, $method, $data, $time, $connection_array, $query);
103 5
    }
104
}
105