Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Version
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 newLog
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch;
4
5use Status;
6
7/**
8 * Fetch the Elasticsearch version
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25class Version extends ElasticsearchIntermediary {
26    /**
27     * @param Connection $conn
28     */
29    public function __construct( Connection $conn ) {
30        parent::__construct( $conn, null, 0 );
31    }
32
33    /**
34     * Get the version of Elasticsearch with which we're communicating.
35     *
36     * @return Status<string> version number as a string
37     */
38    public function get() {
39        try {
40            $this->startNewLog( 'fetching elasticsearch version', 'version' );
41            // If this times out the cluster is in really bad shape but we should still
42            // check it.
43            $this->connection->setTimeout( $this->getClientTimeout( 'version' ) );
44            $result = $this->connection->getClient()->request( '' );
45            $this->success();
46        } catch ( \Elastica\Exception\ExceptionInterface $e ) {
47            return $this->failure( $e );
48        }
49        return Status::newGood(
50            $result->getData()['version']['number']
51        );
52    }
53
54    /**
55     * @param string $description
56     * @param string $queryType
57     * @param string[] $extra
58     * @return SearchRequestLog
59     */
60    protected function newLog( $description, $queryType, array $extra = [] ) {
61        return new SearchRequestLog(
62            $this->connection->getClient(),
63            $description,
64            $queryType,
65            $extra
66        );
67    }
68}