Completed
Push — master ( 8b30ba...c06433 )
by Karel
04:39
created

Client   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 18
lcom 3
cbo 7
dl 0
loc 122
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B request() 0 31 9
A getIndex() 0 8 2
A getIndexTemplate() 0 8 2
A setStopwatch() 0 4 1
A logQuery() 0 19 4
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\Exception\ClientException;
16
use Elastica\Request;
17
use FOS\ElasticaBundle\Logger\ElasticaLogger;
18
use Symfony\Component\Stopwatch\Stopwatch;
19
20
/**
21
 * Extends the default Elastica client to provide logging for errors that occur
22
 * during communication with ElasticSearch.
23
 *
24
 * @author Gordon Franke <[email protected]>
25
 */
26
class Client extends BaseClient
27
{
28
    /**
29
     * Stores created indexes to avoid recreation.
30
     *
31
     * @var array
32
     */
33
    private $indexCache = [];
34
35
    /**
36
     * Stores created index template to avoid recreation.
37
     *
38
     * @var array
39
     */
40
    private $indexTemplateCache = array();
41
42
    /**
43
     * Symfony's debugging Stopwatch.
44
     *
45
     * @var Stopwatch|null
46
     */
47
    private $stopwatch;
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 15
    public function request($path, $method = Request::GET, $data = [], array $query = [], $contentType = Request::DEFAULT_CONTENT_TYPE)
53
    {
54 15
        if ($this->stopwatch) {
55 13
            $this->stopwatch->start('es_request', 'fos_elastica');
56
        }
57
58 15
        $response = parent::request($path, $method, $data, $query, $contentType);
59 15
        $responseData = $response->getData();
60
61 15
        $transportInfo = $response->getTransferInfo();
62 15
        $connection = $this->getLastRequest()->getConnection();
63 15
        $forbiddenHttpCodes = $connection->hasConfig('http_error_codes') ? $connection->getConfig('http_error_codes') : [];
64
65 15
        if (isset($transportInfo['http_code']) && in_array($transportInfo['http_code'], $forbiddenHttpCodes, true)) {
66 1
            $body = is_array($responseData) ? json_encode($responseData) : $responseData;
67 1
            $message = sprintf('Error in transportInfo: response code is %s, response body is %s', $transportInfo['http_code'], $body);
68 1
            throw new ClientException($message);
69
        }
70
71 14
        if (isset($responseData['took']) && isset($responseData['hits'])) {
72 2
            $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']);
73
        } else {
74 13
            $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), 0, 0);
75
        }
76
77 14
        if ($this->stopwatch) {
78 13
            $this->stopwatch->stop('es_request');
79
        }
80
81 14
        return $response;
82
    }
83
84
    /**
85
     * @param string $name
86
     *
87
     * @return Index|mixed
88
     */
89 15
    public function getIndex($name)
90
    {
91 15
        if (isset($this->indexCache[$name])) {
92
            return $this->indexCache[$name];
93
        }
94
95 15
        return $this->indexCache[$name] = new Index($this, $name);
96
    }
97
98 6
    public function getIndexTemplate($name)
99
    {
100 6
        if (isset($this->indexTemplateCache[$name])) {
101 1
            return $this->indexTemplateCache[$name];
102
        }
103
104 6
        return $this->indexTemplateCache[$name] = new IndexTemplate($this, $name);
105
    }
106
107
    /**
108
     * Sets a stopwatch instance for debugging purposes.
109
     *
110
     * @param Stopwatch $stopwatch
111
     */
112 17
    public function setStopwatch(Stopwatch $stopwatch = null)
113
    {
114 17
        $this->stopwatch = $stopwatch;
115 17
    }
116
117
    /**
118
     * Log the query if we have an instance of ElasticaLogger.
119
     *
120
     * @param string $path
121
     * @param string $method
122
     * @param array|string $data
123
     * @param array  $query
124
     * @param int    $queryTime
125
     * @param int    $engineMS
126
     * @param int    $itemCount
127
     */
128 14
    private function logQuery($path, $method, $data, array $query, $queryTime, $engineMS = 0, $itemCount = 0)
129
    {
130 14
        if (!$this->_logger or !$this->_logger instanceof ElasticaLogger) {
131
            return;
132
        }
133
134 14
        $connection = $this->getLastRequest()->getConnection();
135
136
        $connectionArray = [
137 14
            'host' => $connection->getHost(),
138 14
            'port' => $connection->getPort(),
139 14
            'transport' => $connection->getTransport(),
140 14
            'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : [],
141
        ];
142
143
        /** @var ElasticaLogger $logger */
144 14
        $logger = $this->_logger;
145 14
        $logger->logQuery($path, $method, $data, $queryTime, $connectionArray, $query, $engineMS, $itemCount);
146 14
    }
147
}
148