ConditionsDecorator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 49
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B decorate() 0 41 8
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