Completed
Push — master ( 6c8c8c...21048a )
by Peter
04:47
created

QueryBuilder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 6
dl 0
loc 122
ccs 40
cts 45
cp 0.8889
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A add() 0 12 3
A count() 0 10 3
A search() 0 10 4
C getParams() 0 42 7
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 http://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel;
14
15
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
16
use Maslosoft\Mangan\Helpers\CollectionNamer;
17
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
18
use Maslosoft\Mangan\Traits\CriteriaAwareTrait;
19
use Maslosoft\Manganel\Helpers\QueryBuilderDecorator;
20
use UnexpectedValueException;
21
22
/**
23
 * QueryBuilder
24
 * @method SearchCriteria getCriteria()
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class QueryBuilder implements CriteriaAwareInterface
28
{
29
30
	use CriteriaAwareTrait;
31
32
	/**
33
	 * Manganel instance
34
	 * @var Manganel
35
	 */
36
	private $manganel = null;
37
38
	/**
39
	 * Annotated model
40
	 * @var AnnotatedInterface[]
41
	 */
42
	private $models = [];
43
44 10
	public function __construct($model = null)
45
	{
46 10
		if (!empty($model))
47
		{
48 10
			$this->models[] = $model;
49
		}
50 10
		if (!empty($model))
51
		{
52 10
			$this->manganel = Manganel::create($model);
53
		}
54
		else
55
		{
56 7
			$this->manganel = Manganel::fly();
57
		}
58 10
	}
59
60 7
	public function add($model)
61
	{
62 7
		if (is_array($model))
63
		{
64
			foreach ($model as $m)
65
			{
66
				$this->models[] = $m;
67
			}
68
			return;
69
		}
70 7
		$this->models[] = $model;
71 7
	}
72
73
	/**
74
	 *
75
	 * @param string $q
76
	 * @return int
77
	 */
78 9
	public function count($q = null)
79
	{
80 9
		$params = $this->getParams($q);
81 9
		$result = $this->manganel->getClient()->count($params);
82 9
		if (empty($result) && empty($result['count']))
83
		{
84
			return 0; // @codeCoverageIgnore
85
		}
86 9
		return $result['count'];
87
	}
88
89
	/**
90
	 * Get search results
91
	 * @param string $q
92
	 * @return array
93
	 */
94 8
	public function search($q = null)
95
	{
96 8
		$params = $this->getParams($q);
97 8
		$result = $this->manganel->getClient()->search($params);
98 5
		if (empty($result) && empty($result['hits']) && empty($result['hits']['hits']))
99
		{
100
			return []; // @codeCoverageIgnore
101
		}
102 5
		return $result['hits']['hits'];
103
	}
104
105 10
	private function getParams($q = null)
106
	{
107 10
		$body = [];
108
		// Try to get query from criteria if empty
109 10
		$criteria = $this->getCriteria();
110 10
		if (empty($criteria))
111
		{
112 10
			$criteria = new SearchCriteria;
113
		}
114 10
		if (!empty($q))
115
		{
116 7
			$criteria->search($q);
117
		}
118
119 10
		$decorator = new QueryBuilderDecorator($this->manganel);
120 10
		$decorator->decorate($body, $criteria);
121
122 10
		if (empty($this->models))
123
		{
124
			$type = '_all';
125
		}
126
		else
127
		{
128 10
			$types = [];
129 10
			foreach ($this->models as $model)
130
			{
131 10
				if (!$model instanceof AnnotatedInterface)
132
				{
133
					throw new UnexpectedValueException(sprintf('Expected `%s` instance, got `%s`', AnnotatedInterface::class, is_object($model) ? get_class($model) : gettype($model)));
134
				}
135 10
				$types[] = CollectionNamer::nameCollection($model);
136
			}
137 10
			$type = implode(',', array_unique($types));
138
		}
139
140
		$params = [
141 10
			'index' => strtolower($this->manganel->index),
142 10
			'type' => $type,
143 10
			'body' => $body
144
		];
145 10
		return $params;
146
	}
147
148
}
149