Completed
Pull Request — master (#917)
by Dmitry
08:52
created

Client::getIndexTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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