Completed
Push — master ( b4b35b...273652 )
by Peter
05:39
created

CriteriaTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 41.18%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 60
ccs 7
cts 17
cp 0.4118
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCriteria() 0 18 3
A setCriteria() 0 17 4
getModel() 0 1 ?
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Traits\DataProvider;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Interfaces\Criteria\DecoratableInterface;
18
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
19
use Maslosoft\Mangan\Interfaces\ModelAwareInterface;
20
21
/**
22
 * CriteriaTrait
23
 *
24
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
25
 */
26
trait CriteriaTrait
27
{
28
29
	/**
30
	 * @var CriteriaInterface
31
	 */
32
	private $criteria;
33
34
	/**
35
	 * Returns the criteria.
36
	 * @return CriteriaInterface the query criteria
37
	 * @since v1.0
38
	 */
39 4
	public function getCriteria()
40
	{
41
		// Initialise empty criteria, so it's always available via this method call.
42 4
		if (empty($this->criteria))
43
		{
44 4
			$className = static::CriteriaClass;
45 4
			if ($this instanceof ModelAwareInterface)
46
			{
47 4
				$model = $this->getModel();
48
			}
49
			else
50
			{
51
				$model = null;
52
			}
53 4
			$this->criteria = new $className(null, $model);
54
		}
55 4
		return $this->criteria;
56
	}
57
58
	/**
59
	 * Sets the query criteria.
60
	 * @param CriteriaInterface|array $criteria the query criteria. Array representing the MongoDB query criteria.
61
	 * @return static
62
	 */
63
	public function setCriteria($criteria)
64
	{
65
		if (is_array($criteria))
66
		{
67
			$className = static::CriteriaClass;
68
			$this->criteria = new $className($criteria);
69
		}
70
		elseif ($criteria instanceof CriteriaInterface)
71
		{
72
			$this->criteria = $criteria;
73
		}
74
		if ($this->criteria instanceof DecoratableInterface)
75
		{
76
			$this->criteria->decorateWith($this->getModel());
77
		}
78
		return $this;
79
	}
80
81
	/**
82
	 * @return AnnotatedInterface
83
	 */
84
	abstract public function getModel();
85
}
86