Completed
Push — master ( 0dfb02...aa2861 )
by Peter
13:57
created

MultiScopeManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 59
ccs 19
cts 23
cp 0.8261
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNewCriteria() 0 7 1
A getModelCriteria() 0 16 3
A getOneModelCriteria() 0 15 3
1
<?php
2
3
namespace Maslosoft\Manganel;
4
5
use Maslosoft\Mangan\Abstracts\AbstractScopeManager;
6
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
7
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface;
8
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface;
9
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
10
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait;
11
12
/**
13
 * Scope Manager with support for many models
14
 *
15
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
16
 */
17
class MultiScopeManager extends AbstractScopeManager implements ScopeManagerInterface,
18
		ModelsAwareInterface
19
{
20
21
	use UniqueModelsAwareTrait;
22
23
	/**
24
	 *
25
	 * @param object $model Base model used for criteria
26
	 * @param object[] $models Additional models used for criteria
27
	 */
28 30
	public function __construct($model, $models = [])
29
	{
30 30
		$this->setModel($model);
31 30
		$this->setModels($models);
32 30
	}
33
34 9
	public function getNewCriteria($criteria = null)
35
	{
36 9
		$newCriteria = new SearchCriteria($criteria);
37 9
		$newCriteria->decorateWith($this->getModel());
38 9
		$newCriteria->setModels($this->getModels());
39 9
		return $newCriteria;
40
	}
41
42 30
	protected function getModelCriteria()
43
	{
44 30
		$criteria = new SearchCriteria;
45
46 30
		foreach ($this->getModels() as $model)
47
		{
48 30
			$criteria->mergeWith($this->getOneModelCriteria($model));
49
		}
50 30
		$criteria->setModels($this->getModels());
51
52 30
		if (empty($criteria))
53
		{
54
			return $this->getNewCriteria();
55
		}
56 30
		return $criteria;
57
	}
58
59 30
	private function getOneModelCriteria($model)
60
	{
61 30
		if ($model instanceof WithCriteriaInterface)
62
		{
63
			return $model->getDbCriteria();
64
		}
65
		elseif ($model instanceof CriteriaAwareInterface)
66
		{
67
			return $model->getCriteria();
68
		}
69
		else
70
		{
71 30
			return new SearchCriteria;
72
		}
73
	}
74
75
}
76