Completed
Push — master ( f26102...541e16 )
by Peter
08:58
created

MultiScopeManager::getOneModelCriteria()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.0416
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 https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel;
14
15
use Maslosoft\Mangan\Abstracts\AbstractScopeManager;
16
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
17
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface;
18
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface;
19
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
20
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait;
21
22
/**
23
 * Scope Manager with support for many models
24
 *
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class MultiScopeManager extends AbstractScopeManager implements ScopeManagerInterface,
28
		ModelsAwareInterface
29
{
30
31
	use UniqueModelsAwareTrait;
32
33
	/**
34
	 *
35
	 * @param object $model Base model used for criteria
36
	 * @param object[] $models Additional models used for criteria
37
	 */
38 55
	public function __construct($model, $models = [])
39
	{
40 55
		$this->setModel($model);
41 55
		$this->setModels($models);
42 55
	}
43
44 9
	public function getNewCriteria($criteria = null)
45
	{
46 9
		$newCriteria = new SearchCriteria($criteria);
47 9
		$newCriteria->decorateWith($this->getModel());
48 9
		$newCriteria->setModels($this->getModels());
49 9
		return $newCriteria;
50
	}
51
52 55
	protected function getModelCriteria()
53
	{
54 55
		$criteria = new SearchCriteria;
55
56 55
		foreach ($this->getModels() as $model)
57
		{
58 55
			$criteria->mergeWith($this->getOneModelCriteria($model));
59
		}
60 55
		$criteria->setModels($this->getModels());
61
62 55
		if (empty($criteria))
63
		{
64
			return $this->getNewCriteria();
65
		}
66 55
		return $criteria;
67
	}
68
69 55
	private function getOneModelCriteria($model)
70
	{
71 55
		if ($model instanceof WithCriteriaInterface)
72
		{
73 1
			return $model->getDbCriteria();
74
		}
75 54
		elseif ($model instanceof CriteriaAwareInterface)
76
		{
77
			return $model->getCriteria();
78
		}
79
		else
80
		{
81 54
			return new SearchCriteria;
82
		}
83
	}
84
85
}
86