Completed
Push — master ( 2868b6...82c634 )
by GBProd
03:14
created

Client::request()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
rs 9.4285
cc 3
eloc 9
nc 2
nop 4
crap 3.0123
1
<?php
2
3
namespace GBProd\ElasticaBundle\Elastica;
4
5
use Elastica\Client as BaseClient;
6
use Elastica\Request;
7
use GBProd\ElasticaBundle\Logger\ElasticaLogger;
8
9
/**
10
 * Extends the default Elastica client to provide logging for errors that occur
11
 * during communication with ElasticSearch.
12
 *
13
 * @author Gordon Franke <[email protected]>
14
 */
15
class Client extends BaseClient
16
{
17
    /**
18
     * @param string $path
19
     * @param string $method
20
     * @param array  $data
21
     * @param array  $query
22
     *
23
     * @return \Elastica\Response
24
     */
25 1
    public function request($path, $method = Request::GET, $data = array(), array $query = array())
26
    {
27 1
        $start = microtime(true);
28 1
        $response = parent::request($path, $method, $data, $query);
29 1
        $responseData = $response->getData();
30
31 1
        if (isset($responseData['took']) && isset($responseData['hits'])) {
32 1
            $this->logQuery($path, $method, $data, $query, $start, $response->getEngineTime(), $responseData['hits']['total']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
33 1
        } else {
34
            $this->logQuery($path, $method, $data, $query, $start, 0, 0);
35
        }
36
37 1
        return $response;
38
    }
39
40
    /**
41
     * Log the query if we have an instance of ElasticaLogger.
42
     *
43
     * @param string $path
44
     * @param string $method
45
     * @param array  $data
46
     * @param array  $query
47
     * @param int    $start
48
     */
49 1
    private function logQuery($path, $method, $data, array $query, $start, $engineMS = 0, $itemCount = 0)
50
    {
51 1
        if (!$this->_logger or !$this->_logger instanceof ElasticaLogger) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
52
            return;
53
        }
54
55 1
        $time = microtime(true) - $start;
56 1
        $connection = $this->getLastRequest()->getConnection();
57
58
        $connection_array = array(
59 1
            'host' => $connection->getHost(),
60 1
            'port' => $connection->getPort(),
61 1
            'transport' => $connection->getTransport(),
62 1
            'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : array(),
63 1
        );
64
65 1
        $this->_logger->logQuery($path, $method, $data, $time, $connection_array, $query, $engineMS, $itemCount);
66 1
    }
67
}
68