Completed
Push — multimodel-dp ( 745c1d...bcd65b )
by Peter
03:53
created

ElasticSearchCursor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 37.78%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 115
ccs 17
cts 45
cp 0.3778
rs 10
c 0
b 0
f 0

12 Methods

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