ConditionsDecorator::decorate()   B
last analyzed

Complexity

Conditions 8
Paths 18

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.006

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 21
cts 22
cp 0.9545
rs 8.0195
c 0
b 0
f 0
cc 8
nc 18
nop 2
crap 8.006
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\BodyDecoratorInterface;
18
use Maslosoft\Manganel\Interfaces\QueryBuilder\ConditionDecoratorInterface;
19
use Maslosoft\Manganel\SearchCriteria;
20
use Maslosoft\Manganel\Traits\ManganelAwareTrait;
21
22
/**
23
 * This decorator provides filtering capabilities for query builder
24
 *
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class ConditionsDecorator implements BodyDecoratorInterface,
28
		ManganelAwareInterface
29
{
30
31
	use ManganelAwareTrait;
32
33 32
	public function decorate(&$body, SearchCriteria $criteria)
34
	{
35 32
		$decorators = (new PluginFactory())->instance($this->getManganel()->decorators, $criteria, [
36 32
			ConditionDecoratorInterface::class
37
		]);
38
39 32
		$bool = [];
40 32
		foreach ($decorators as $decorator)
41
		{
42
			/* @var $decorator ConditionDecoratorInterface  */
43 32
			if ($decorator instanceof ManganelAwareInterface)
44
			{
45 32
				$decorator->setManganel($this->getManganel());
46
			}
47 32
			$conditions = [];
48 32
			$decorator->decorate($conditions, $criteria);
49 32
			if (empty($conditions))
50
			{
51 32
				continue;
52
			}
53 32
			$kind = $decorator->getKind();
54 32
			if(false === $kind)
55
			{
56
				continue;
57
			}
58 32
			if (empty($bool[$kind]))
59
			{
60 32
				$bool[$kind] = [];
61
			}
62 32
			foreach($conditions as $condition)
63
			{
64 32
				$bool[$kind][] = $condition;
65
			}
66
		}
67 32
		if (!empty($bool))
68
		{
69 32
			$body['query'] = [
70 32
				'bool' => $bool
71
			];
72
		}
73 32
	}
74
75
}
76