Completed
Pull Request — master (#208)
by
unknown
03:51
created

ElasticsearchAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 72
ccs 0
cts 27
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNbResults() 0 7 2
A getSlice() 0 8 1
A runQuery() 0 7 2
1
<?php
2
3
namespace Pagerfanta\Adapter;
4
5
use Elasticsearch\Client;
6
7
/**
8
 * ElasticsearchAdapter.
9
 *
10
 * Used with the official Elasticsearch PHP library.
11
 * 
12
 * @author Danny Sipos <[email protected]>
13
 */
14
class ElasticsearchAdapter implements AdapterInterface
15
{
16
17
  /**
18
   * The Elasticsearch query parameters.
19
   *
20
   * @var array
21
   */
22
  private $params;
23
24
  /**
25
   * The Elasticsearch results
26
   *
27
   * @var array
28
   */
29
  private $results;
30
31
  /**
32
   * The Elasticsearch client.
33
   *
34
   * @var Client
35
   */
36
  private $client;
37
38
  /**
39
   * Constructor.
40
   *
41
   * @param array $params The array of parameters to use for performing the search.
42
   * @param Client $client The Elasticsearch client.
43
   */
44
  public function __construct(array $params, $client)
45
  {
46
    $this->params = $params;
47
    $this->client = $client;
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function getNbResults()
54
  {
55
    if (!$this->results) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
56
      $this->runQuery();
57
    }
58
    return $this->results['hits']['total'];
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function getSlice($offset, $length)
65
  {
66
    $params = $this->params;
67
    $params['from'] = $offset;
68
    $params['size'] = $length;
69
    $this->runQuery($params);
70
    return $this->results;
71
  }
72
73
  /**
74
   * Runs the query and stores the results.
75
   *
76
   * @param array $params
77
   */
78
  private function runQuery($params = array())
79
  {
80
    if (!$params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
      $params = $this->params;
82
    }
83
    $this->results = $this->client->search($params);
84
  }
85
}
86