Completed
Pull Request — master (#970)
by
unknown
06:31
created

Client::getIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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 9
    public function request($path, $method = Request::GET, $data = array(), array $query = array())
41
    {
42 9
        if ($this->stopwatch) {
43 8
            $this->stopwatch->start('es_request', 'fos_elastica');
44 8
        }
45
46 9
        $start = microtime(true);
47 9
        $response = parent::request($path, $method, $data, $query);
48
49 9
        if (isset($response->getData()['took'])) {
50 4
            $this->logQuery($path, $method, $data, $query, $start, $response->getEngineTime(), $response->getData()['hits']['total']);
51 1
        } else {
52 5
            $this->logQuery($path, $method, $data, $query, $start, 0, 0);
53
        }
54
55 6
        if ($this->stopwatch) {
56 5
            $this->stopwatch->stop('es_request');
57 5
        }
58
59 6
        return $response;
60
    }
61
62 10
    public function getIndex($name)
63
    {
64 10
        if (isset($this->indexCache[$name])) {
65
            return $this->indexCache[$name];
66
        }
67
68 10
        return $this->indexCache[$name] = new Index($this, $name);
69
    }
70
71
    /**
72
     * Sets a stopwatch instance for debugging purposes.
73
     *
74
     * @param Stopwatch $stopwatch
75
     */
76 11
    public function setStopwatch(Stopwatch $stopwatch = null)
77
    {
78 11
        $this->stopwatch = $stopwatch;
79 11
    }
80
81
    /**
82
     * Log the query if we have an instance of ElasticaLogger.
83
     *
84
     * @param string $path
85
     * @param string $method
86
     * @param array  $data
87
     * @param array  $query
88
     * @param int    $start
89
     */
90 6
    private function logQuery($path, $method, $data, array $query, $start, $engineMS = 0, $itemCount = 0)
91
    {
92 6
        if (!$this->_logger or !$this->_logger instanceof ElasticaLogger) {
93
            return;
94
        }
95
96 6
        $time = microtime(true) - $start;
97 6
        $connection = $this->getLastRequest()->getConnection();
98
99
        $connection_array = array(
100 6
            'host' => $connection->getHost(),
101 6
            'port' => $connection->getPort(),
102 6
            'transport' => $connection->getTransport(),
103 6
            'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : array(),
104 6
        );
105
106 6
        $this->_logger->logQuery($path, $method, $data, $time, $connection_array, $query, $engineMS, $itemCount);
107 6
    }
108
}
109