Completed
Push — master ( 05c57d...e1240a )
by Tim
14:32 queued 03:43
created

Client::request()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0145

Importance

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