Completed
Pull Request — master (#1149)
by Oleg
06:13
created

Client::setStopwatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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