Completed
Push — master ( 616fda...ede5da )
by Peter
06:14
created

QueryBuilder::getParams()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 67
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.0099

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 67
ccs 16
cts 17
cp 0.9412
rs 7.0237
cc 7
eloc 22
nc 12
nop 1
crap 7.0099

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
				'simple_query_string' => [
99 6
					'query' => $q
100
				]
101
			];
102
		}
103
104
//
105
//		  TODO: Build somewhat similar query, if criteria has
106
//		  ANY conditions. Add conditions to `filter` clause:
107
//		  {
108
//				"query": {
109
//					"filtered": {
110
//						"query": {
111
//							"query_string": {
112
//								"query": "jkow OR features"
113
//							}
114
//						},
115
//						"filter": {
116
//							"term": {
117
//								"published.en": true
118
//							}
119
//						}
120
//					}
121
//				}
122
//			}
123
//
124
125
126 7
		$body['query'] = $query;
127
128 7
		if (!empty($criteria))
129
		{
130 4
			if ($criteria->getLimit() || $criteria->getOffset())
131
			{
132 4
				$body['from'] = $criteria->getOffset();
133 4
				$body['size'] = $criteria->getLimit();
134
			}
135
		}
136
137
		$params = [
138 7
			'index' => strtolower($this->manganel->index),
139 7
			'type' => CollectionNamer::nameCollection($this->model),
140 7
			'body' => $body
141
		];
142 7
		return $params;
143
	}
144
145
}
146