Completed
Push — master ( 7d6c96...5af7de )
by Peter
05:54
created

ElasticSearchCursor::fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Maslosoft\Manganel\Adapters\Finder;
4
5
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface;
6
use Maslosoft\Manganel\Decorators\IndexDecorator;
7
use Maslosoft\Manganel\Decorators\ScoreDecorator;
8
use Maslosoft\Manganel\QueryBuilder;
9
use Maslosoft\Manganel\SearchCriteria;
10
11
/**
12
 * ElasticSearchCursor
13
 *
14
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
15
 */
16
class ElasticSearchCursor implements FinderCursorInterface
17
{
18
19
	/**
20
	 * Whether query was sent to search server
21
	 * @var bool
22
	 */
23
	private $isExecuted = false;
24
25
	/**
26
	 *
27
	 * @var QueryBuilder
28
	 */
29
	private $qb = null;
30
31
	/**
32
	 *
33
	 * @var array
34
	 */
35
	private $data = [];
36
37 25
	public function __construct(QueryBuilder $qb)
38
	{
39 25
		$this->qb = $qb;
40 25
		$criteria = $qb->getCriteria();
41 25
		if (empty($criteria))
42
		{
43
			$this->qb->setCriteria(new SearchCriteria());
44
		}
45 25
	}
46
47 14
	public function limit($num)
48
	{
49 14
		$this->qb->getCriteria()->limit($num);
50 14
		return $this;
51
	}
52
53 9
	public function skip($num)
54
	{
55 9
		$this->qb->getCriteria()->offset($num);
56 9
		return $this;
57
	}
58
59 15
	public function sort(array $fields)
60
	{
61 15
		$this->qb->getCriteria()->setSort($fields);
62 15
		return $this;
63
	}
64
65
	public function fields(array $fields)
66
	{
67
		$this->qb->getCriteria()->select($fields);
68
		return $this;
69
	}
70
71
// <editor-fold defaultstate="collapsed" desc="Countable impl">
72
73
	/**
74
	 *
75
	 * @return int
76
	 */
77 6
	public function count()
78
	{
79 6
		$this->execute();
80 6
		return count($this->data);
81
	}
82
83
// </editor-fold>
84
// <editor-fold defaultstate="collapsed" desc="Iterator impl">
85 20
	public function current()
86
	{
87 20
		$this->execute();
88 20
		return current($this->data);
89
	}
90
91
	public function key()
92
	{
93
		$this->execute();
94
		return key($this->data);
95
	}
96
97 15
	public function next()
98
	{
99 15
		$this->execute();
100 15
		next($this->data);
101 15
	}
102
103 15
	public function rewind()
104
	{
105 15
		$this->execute();
106 15
		return reset($this->data);
107
	}
108
109 15
	public function valid()
110
	{
111 15
		$this->execute();
112 15
		$key = key($this->data);
113 15
		return ($key !== null && $key !== false);
114
	}
115
116 25
	private function execute()
117
	{
118 25
		if (!$this->isExecuted)
119
		{
120 25
			$this->isExecuted = true;
121 25
			$data = $this->qb->search();
122 25
			foreach ($data as $result)
123
			{
124 25
				$document = $result['_source'];
125 25
				$document[IndexDecorator::Key] = $result['_index'];
126 25
				$document[ScoreDecorator::Key] = $result['_score'];
127 25
				$this->data[] = $document;
128
			}
129
		}
130 25
	}
131
132
// </editor-fold>
133
}
134