Completed
Push — master ( 451970...c83d03 )
by Karel
04:43
created

Client::logQuery()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

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