SearchDecorator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 53
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getKind() 0 4 1
A decorate() 0 38 5
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Decorators\QueryBuilder;
14
15
use Maslosoft\Gazebo\PluginFactory;
16
use Maslosoft\Manganel\Interfaces\ManganelAwareInterface;
17
use Maslosoft\Manganel\Interfaces\QueryBuilder\ConditionDecoratorInterface;
18
use Maslosoft\Manganel\Interfaces\QueryBuilder\QueryStringDecoratorInterface;
19
use Maslosoft\Manganel\SearchCriteria;
20
use Maslosoft\Manganel\Traits\ManganelAwareTrait;
21
use stdClass;
22
23
/**
24
 * SearchDecorator
25
 *
26
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
27
 */
28
class SearchDecorator implements ConditionDecoratorInterface,
29
		ManganelAwareInterface
30
{
31
32
	use ManganelAwareTrait;
33
34
	const Ns = __NAMESPACE__;
35
36 32
	public function decorate(&$conditions, SearchCriteria $criteria)
37
	{
38 32
		$q = $criteria->getSearch();
39
40 32
		if (empty($q))
41
		{
42 8
			if(!empty($criteria->getConditions()))
43
			{
44 2
				return;
45
			}
46 7
			if(!empty($criteria->getMoreLike()))
47
			{
48
				return;
49
			}
50
			// Match all documents if query is null
51
			// stdClass is used here to get `{}` in json, as `[]` causes bad
52
			// request exception!
53 7
			$conditions[] = [
54 7
				'match_all' => new stdClass()
55
			];
56
		}
57
		else
58
		{
59
			// Use query string matching
60 24
			$decorators = (new PluginFactory())->instance($this->manganel->decorators, $criteria, [
61 24
				QueryStringDecoratorInterface::class
62
			]);
63 24
			$queryStringParams = [];
64 24
			foreach ($decorators as $decorator)
65
			{
66
				/* @var $decorator QueryStringDecoratorInterface */
67 24
				$decorator->decorate($queryStringParams, $criteria);
68
			}
69 24
			$conditions[] = [
70 24
				'simple_query_string' => $queryStringParams
71
			];
72
		}
73 31
	}
74
75 31
	public function getKind()
76
	{
77 31
		return self::KindMust;
78
	}
79
80
}
81