Completed
Pull Request — master (#1343)
by Dmitry
05:49
created

Client::getIndexTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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