Completed
Push — master ( f26102...541e16 )
by Peter
08:58
created

ElasticSearchCursor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.16%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 128
ccs 47
cts 51
cp 0.9216
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A skip() 0 5 1
A __construct() 0 9 2
A limit() 0 5 1
A sort() 0 5 1
A fields() 0 5 1
A count() 0 5 1
A current() 0 5 1
A key() 0 5 1
A next() 0 5 1
A rewind() 0 5 1
A valid() 0 6 2
B execute() 0 25 3
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Adapters\Finder;
14
15
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface;
16
use Maslosoft\Manganel\Decorators\IndexDecorator;
17
use Maslosoft\Manganel\Decorators\MaxScoreDecorator;
18
use Maslosoft\Manganel\Decorators\ScoreDecorator;
19
use Maslosoft\Manganel\QueryBuilder;
20
use Maslosoft\Manganel\SearchCriteria;
21
22
/**
23
 * ElasticSearchCursor
24
 *
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class ElasticSearchCursor implements FinderCursorInterface
28
{
29
30
	/**
31
	 * Whether query was sent to search server
32
	 * @var bool
33
	 */
34
	private $isExecuted = false;
35
36
	/**
37
	 *
38
	 * @var QueryBuilder
39
	 */
40
	private $qb = null;
41
42
	/**
43
	 *
44
	 * @var array
45
	 */
46
	private $data = [];
47
48 52
	public function __construct(QueryBuilder $qb)
49
	{
50 52
		$this->qb = $qb;
51 52
		$criteria = $qb->getCriteria();
52 52
		if (empty($criteria))
53
		{
54
			$this->qb->setCriteria(new SearchCriteria());
55
		}
56 52
	}
57
58 41
	public function limit($num)
59
	{
60 41
		$this->qb->getCriteria()->limit($num);
61 41
		return $this;
62
	}
63
64 36
	public function skip($num)
65
	{
66 36
		$this->qb->getCriteria()->offset($num);
67 36
		return $this;
68
	}
69
70 42
	public function sort(array $fields)
71
	{
72 42
		$this->qb->getCriteria()->setSort($fields);
73 42
		return $this;
74
	}
75
76 1
	public function fields(array $fields)
77
	{
78 1
		$this->qb->getCriteria()->select($fields);
79 1
		return $this;
80
	}
81
82
// <editor-fold defaultstate="collapsed" desc="Countable impl">
83
84
	/**
85
	 *
86
	 * @return int
87
	 */
88 6
	public function count()
89
	{
90 6
		$this->execute();
91 6
		return count($this->data);
92
	}
93
94
// </editor-fold>
95
// <editor-fold defaultstate="collapsed" desc="Iterator impl">
96 43
	public function current()
97
	{
98 43
		$this->execute();
99 43
		return current($this->data);
100
	}
101
102
	public function key()
103
	{
104
		$this->execute();
105
		return key($this->data);
106
	}
107
108 38
	public function next()
109
	{
110 38
		$this->execute();
111 38
		next($this->data);
112 38
	}
113
114 42
	public function rewind()
115
	{
116 42
		$this->execute();
117 42
		return reset($this->data);
118
	}
119
120 42
	public function valid()
121
	{
122 42
		$this->execute();
123 42
		$key = key($this->data);
124 42
		return ($key !== null && $key !== false);
125
	}
126
127 52
	private function execute()
128
	{
129 52
		if (!$this->isExecuted)
130
		{
131 52
			$this->isExecuted = true;
132 52
			$results = [];
133 52
			$data = $this->qb->search(null, $results);
134 52
			$maxScore = $results['hits']['max_score'];
135 52
			foreach ($data as $result)
136
			{
137 47
				$document = $result['_source'];
138
				/**
139
				 * TODO Refactor it into plugable interface, with params:
140
				 * $document,
141
				 * $result,
142
				 * $results
143
				 */
144 47
				$document[IndexDecorator::Key] = $result['_index'];
145 47
				$document[ScoreDecorator::Key] = $result['_score'];
146 47
				$document[MaxScoreDecorator::Key] = $maxScore;
147
148 47
				$this->data[] = $document;
149
			}
150
		}
151 52
	}
152
153
// </editor-fold>
154
}
155