Completed
Pull Request — master (#1413)
by
unknown
05:55
created

Client   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 94.87%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 5
dl 0
loc 112
ccs 37
cts 39
cp 0.9487
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C request() 0 36 7
A getIndex() 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\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
     * {@inheritdoc}
43
     */
44 10
    public function request($path, $method = Request::GET, $data = [], array $query = [], $contentType = Request::DEFAULT_CONTENT_TYPE)
45
    {
46 10
        if ($this->stopwatch) {
47 9
            $this->stopwatch->start('es_request', 'fos_elastica');
48
        }
49
50 10
        $exception = null;
51 10
        $responseData = [];
52 10
        $queryTime = 0;
53 10
        $engineTime = 0;
54
55
        try {
56 10
            $response = parent::request($path, $method, $data, $query, $contentType);
57 10
            $responseData = $response->getData();
58 10
            $queryTime = $response->getQueryTime();
59 10
            $engineTime = $response->getEngineTime();
60 7
        } catch (\Exception $e) {
61 7
            $exception = $e;
62
        }
63
64 10
        if (isset($responseData['took']) && isset($responseData['hits'])) {
65 1
            $this->logQuery($path, $method, $data, $query, $queryTime, $engineTime, $responseData['hits']['total'], $exception);
66
        } else {
67 9
            $this->logQuery($path, $method, $data, $query, $queryTime, 0, 0, $exception);
68
        }
69
70 10
        if ($this->stopwatch) {
71 9
            $this->stopwatch->stop('es_request');
72
        }
73
74 10
        if ($exception) {
75 7
            throw $exception;
76
        }
77
78 5
        return $response;
79
    }
80
81
    /**
82
     * @param string $name
83
     *
84
     * @return Index|mixed
85
     */
86 12
    public function getIndex($name)
87
    {
88 12
        if (isset($this->indexCache[$name])) {
89
            return $this->indexCache[$name];
90
        }
91
92 12
        return $this->indexCache[$name] = new Index($this, $name);
93
    }
94
95
    /**
96
     * Sets a stopwatch instance for debugging purposes.
97
     *
98
     * @param Stopwatch $stopwatch
99
     */
100 13
    public function setStopwatch(Stopwatch $stopwatch = null)
101
    {
102 13
        $this->stopwatch = $stopwatch;
103 13
    }
104
105
    /**
106
     * Log the query if we have an instance of ElasticaLogger.
107
     *
108
     * @param string          $path
109
     * @param string          $method
110
     * @param array           $data
111
     * @param array           $query
112
     * @param int             $queryTime
113
     * @param int             $engineMS
114
     * @param int             $itemCount
115
     * @oaram \Exception|null $exception
116
     */
117 10
    private function logQuery($path, $method, $data, array $query, $queryTime, $engineMS = 0, $itemCount = 0, \Exception $exception = null)
118
    {
119 10
        if (!$this->_logger or !$this->_logger instanceof ElasticaLogger) {
120
            return;
121
        }
122
123 10
        $connection = $this->getLastRequest()->getConnection();
124
125
        $connectionArray = [
126 10
            'host' => $connection->getHost(),
127 10
            'port' => $connection->getPort(),
128 10
            'transport' => $connection->getTransport(),
129 10
            'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : [],
130
        ];
131
132
        /** @var ElasticaLogger $logger */
133 10
        $logger = $this->_logger;
134 10
        $logger->logQuery($path, $method, $data, $queryTime, $connectionArray, $query, $engineMS, $itemCount, $exception);
135 10
    }
136
}
137