MultiModelDecorator::decorate()   B
last analyzed

Complexity

Conditions 6
Paths 14

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.0146

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 25
cts 27
cp 0.9259
rs 8.4032
c 0
b 0
f 0
cc 6
nc 14
nop 2
crap 6.0146

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
 * 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\Mangan\Interfaces\CriteriaAwareInterface;
16
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface;
17
use Maslosoft\Manganel\Helpers\TypeNamer;
18
use Maslosoft\Manganel\Interfaces\ManganelAwareInterface;
19
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
20
use Maslosoft\Manganel\SearchCriteria;
21
use Maslosoft\Manganel\Traits\ManganelAwareTrait;
22
use Maslosoft\Manganel\Traits\ModelsAwareTrait;
23
24
/**
25
 * MultiModelDecorator
26
 *
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
class MultiModelDecorator implements ManganelAwareInterface,
30
		ModelsAwareInterface
31
{
32
33
	use ManganelAwareTrait,
34
	  ModelsAwareTrait;
35
36 6
	public function __construct($models)
37
	{
38 6
		$this->models = $models;
39 6
	}
40
41 6
	public function decorate(&$body, SearchCriteria $initialCriteria)
42
	{
43 6
		$queries = [];
44 6
		foreach ($this->models as $model)
45
		{
46 6
			$modelCriteria = null;
47 6
			if ($model instanceof CriteriaAwareInterface)
48
			{
49
				$modelCriteria = $model->getCriteria();
50
			}
51 6
			elseif ($model instanceof WithCriteriaInterface)
52
			{
53 2
				$modelCriteria = $model->getDbCriteria(false);
54
			}
55 6
			if (empty($modelCriteria))
56
			{
57 4
				$criteria = $initialCriteria;
58
			}
59
			else
60
			{
61 2
				$criteria = clone $initialCriteria;
62 2
				$criteria->mergeWith($modelCriteria);
63
			}
64 6
			$criteria->setModel($model);
65 6
			$partial = [];
66 6
			(new SingleModelDecorator())
67 6
					->setManganel($this->manganel)
68 6
					->decorate($partial, $criteria);
69
70
			$query = [
71
				'bool' => [
72
					'filter' => [
73
						'type' => [
74 6
							'value' => TypeNamer::nameType($model)
75
						]
76
					],
77 6
					'must' => $partial['query']
78
				]
79
			];
80 6
			$queries[] = $query;
81
		}
82 6
		$body['query']['dis_max']['queries'] = $queries;
83
84 6
		$common = [];
85 6
		(new SingleModelDecorator)->setManganel($this->getManganel())->decorate($common, $initialCriteria);
86 6
		unset($common['query']);
87
88
		// Use foreach here, as $body is passed by ref
89 6
		foreach ($common as $key => $value)
90
		{
91
			$body[$key] = $value;
92
		}
93 6
	}
94
95
}
96