Completed
Push — master ( 3cdfc2...4aa078 )
by Maksim
03:09
created

Client   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 5
dl 0
loc 96
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A logQuery() 0 19 4
B request() 0 21 5
A getIndex() 0 8 2
A setStopwatch() 0 4 1
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
        $response = parent::request($path, $method, $data, $query, $contentType);
51 10
        $responseData = $response->getData();
52
53 10
        if (isset($responseData['took']) && isset($responseData['hits'])) {
54 2
            $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']);
55
        } else {
56 9
            $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), 0, 0);
57
        }
58
59 10
        if ($this->stopwatch) {
60 9
            $this->stopwatch->stop('es_request');
61
        }
62
63 10
        return $response;
64
    }
65
66
    /**
67
     * @param string $name
68
     *
69
     * @return Index|mixed
70
     */
71 12
    public function getIndex($name)
72
    {
73 12
        if (isset($this->indexCache[$name])) {
74
            return $this->indexCache[$name];
75
        }
76
77 12
        return $this->indexCache[$name] = new Index($this, $name);
78
    }
79
80
    /**
81
     * Sets a stopwatch instance for debugging purposes.
82
     *
83
     * @param Stopwatch $stopwatch
84
     */
85 13
    public function setStopwatch(Stopwatch $stopwatch = null)
86
    {
87 13
        $this->stopwatch = $stopwatch;
88 13
    }
89
90
    /**
91
     * Log the query if we have an instance of ElasticaLogger.
92
     *
93
     * @param string $path
94
     * @param string $method
95
     * @param array  $data
96
     * @param array  $query
97
     * @param int    $queryTime
98
     * @param int    $engineMS
99
     * @param int    $itemCount
100
     */
101 10
    private function logQuery($path, $method, $data, array $query, $queryTime, $engineMS = 0, $itemCount = 0)
102
    {
103 10
        if (!$this->_logger or !$this->_logger instanceof ElasticaLogger) {
104
            return;
105
        }
106
107 10
        $connection = $this->getLastRequest()->getConnection();
108
109
        $connectionArray = [
110 10
            'host' => $connection->getHost(),
111 10
            'port' => $connection->getPort(),
112 10
            'transport' => $connection->getTransport(),
113 10
            'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : [],
114
        ];
115
116
        /** @var ElasticaLogger $logger */
117 10
        $logger = $this->_logger;
118 10
        $logger->logQuery($path, $method, $data, $queryTime, $connectionArray, $query, $engineMS, $itemCount);
119 10
    }
120
}
121