Completed
Push — master ( 7d6c96...5af7de )
by Peter
05:54
created

MultiScopeManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 57
ccs 17
cts 21
cp 0.8095
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getModelCriteria() 0 15 3
A getOneModelCriteria() 0 15 3
A getNewCriteria() 0 6 1
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
 * TODO, maybe it's not even necessary? Will be known when SearchFinder will be
16
 * implemented...
17
 *
18
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
19
 */
20
class MultiScopeManager extends AbstractScopeManager implements ScopeManagerInterface,
21
		ModelsAwareInterface
22
{
23
24
	use UniqueModelsAwareTrait;
25
26
	/**
27
	 *
28
	 * @param object $model Base model used for criteria
29
	 * @param object[] $models Additional models used for criteria
30
	 */
31 28
	public function __construct($model, $models = [])
32
	{
33 28
		$this->setModel($model);
34 28
		$this->setModels($models);
35 28
	}
36
37 9
	public function getNewCriteria($criteria = null)
38
	{
39 9
		$newCriteria = new SearchCriteria($criteria);
40 9
		$newCriteria->decorateWith($this->getModel());
41 9
		return $newCriteria;
42
	}
43
44 28
	protected function getModelCriteria()
45
	{
46 28
		$criteria = new SearchCriteria;
47
48 28
		foreach ($this->getModels() as $model)
49
		{
50 28
			$criteria->mergeWith($this->getOneModelCriteria($model));
51
		}
52
53 28
		if (empty($criteria))
54
		{
55
			return $this->getNewCriteria();
56
		}
57 28
		return $criteria;
58
	}
59
60 28
	private function getOneModelCriteria($model)
61
	{
62 28
		if ($model instanceof WithCriteriaInterface)
63
		{
64
			return $model->getDbCriteria();
65
		}
66
		elseif ($model instanceof CriteriaAwareInterface)
67
		{
68
			return $model->getCriteria();
69
		}
70
		else
71
		{
72 28
			return new SearchCriteria;
73
		}
74
	}
75
76
}
77