Completed
Push — master ( 095fa7...616fda )
by Peter
05:41
created

QueryBuilder::getParams()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.0099

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 44
ccs 16
cts 17
cp 0.9412
rs 6.7272
cc 7
eloc 22
nc 12
nop 1
crap 7.0099
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Manganel;
10
11
use Exception;
12
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
13
use Maslosoft\Mangan\Helpers\CollectionNamer;
14
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
15
use Maslosoft\Mangan\Traits\CriteriaAwareTrait;
16
17
/**
18
 * QueryBuilder
19
 * @method SearchCriteria getCriteria()
20
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
21
 */
22
class QueryBuilder implements CriteriaAwareInterface
23
{
24
25
	use CriteriaAwareTrait;
26
27
	/**
28
	 * Manganel instance
29
	 * @var Manganel
30
	 */
31
	private $manganel = null;
32
33
	/**
34
	 * Annotated model
35
	 * @var AnnotatedInterface
36
	 */
37
	private $model;
38
39 7
	public function __construct($model)
40
	{
41 7
		$this->model = $model;
42 7
		$this->manganel = Manganel::create($this->model);
43 7
	}
44
45
	/**
46
	 *
47
	 * @param string $q
48
	 * @return int
49
	 */
50 6
	public function count($q = null)
51
	{
52 6
		$params = $this->getParams($q);
53 6
		$result = $this->manganel->getClient()->count($params);
54 6
		if (empty($result) && empty($result['count']))
55
		{
56
			return 0; // @codeCoverageIgnore
57
		}
58 6
		return $result['count'];
59
	}
60
61
	/**
62
	 * Get search results
63
	 * @param string $q
64
	 * @return array
65
	 */
66 5
	public function search($q = null)
67
	{
68 5
		$params = $this->getParams($q);
69 5
		$result = $this->manganel->getClient()->search($params);
70 5
		if (empty($result) && empty($result['hits']) && empty($result['hits']['hits']))
71
		{
72
			return []; // @codeCoverageIgnore
73
		}
74 5
		return $result['hits']['hits'];
75
	}
76
77 7
	private function getParams($q = null)
78
	{
79 7
		$body = [];
80
		// Try to get query from criteria if empty
81 7
		$criteria = $this->getCriteria();
82 7
		if (null === $q && !empty($criteria))
83
		{
84
			$q = $criteria->getSearch();
85
		}
86
87 7
		if (null === $q)
88
		{
89
			// Match all documents if query is null
90
			$query = [
91
				'match_all' => []
92 1
			];
93
		}
94
		else
95
		{
96
			// Use query string matching
97
			$query = [
98
				'query_string' => [
99 6
					'query' => $q
100
				]
101
			];
102
		}
103 7
		$body['query'] = $query;
104
105 7
		if (!empty($criteria))
106
		{
107 4
			if ($criteria->getLimit() || $criteria->getOffset())
108
			{
109 4
				$body['from'] = $criteria->getOffset();
110 4
				$body['size'] = $criteria->getLimit();
111
			}
112
		}
113
114
		$params = [
115 7
			'index' => strtolower($this->manganel->index),
116 7
			'type' => CollectionNamer::nameCollection($this->model),
117 7
			'body' => $body
118
		];
119 7
		return $params;
120
	}
121
122
}
123