Completed
Push — master ( 144202...6a00f8 )
by Peter
11:59
created

CriteriaTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 48
ccs 7
cts 21
cp 0.3333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCriteria() 0 10 2
A setCriteria() 0 17 4
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Traits\DataProvider;
10
11
use Maslosoft\Mangan\Criteria;
12
use Maslosoft\Mangan\Interfaces\Criteria\DecoratableInterface;
13
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
14
15
/**
16
 * CriteriaTrait
17
 *
18
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
19
 */
20
trait CriteriaTrait
21
{
22
23
	/**
24
	 * @var CriteriaInterface
25
	 */
26
	private $criteria;
27
28
	/**
29
	 * Returns the criteria.
30
	 * @return CriteriaInterface the query criteria
31
	 * @since v1.0
32
	 */
33 1
	public function getCriteria()
34
	{
35
		// Initialise empty criteria, so it's always available via this method call.
36 1
		if (empty($this->criteria))
37 1
		{
38 1
			$className = static::CriteriaClass;
39 1
			$this->criteria = new $className;
40 1
		}
41 1
		return $this->criteria;
42
	}
43
44
	/**
45
	 * Sets the query criteria.
46
	 * @param CriteriaInterface|array $criteria the query criteria. Array representing the MongoDB query criteria.
47
	 * @return static
48
	 */
49
	public function setCriteria($criteria)
50
	{
51
		if (is_array($criteria))
52
		{
53
			$className = static::CriteriaClass;
54
			$this->criteria = new $className($criteria);
55
		}
56
		elseif ($criteria instanceof CriteriaInterface)
57
		{
58
			$this->criteria = $criteria;
59
		}
60
		if ($this->criteria instanceof DecoratableInterface)
61
		{
62
			$this->criteria->decorateWith($this->getModel());
0 ignored issues
show
Bug introduced by
It seems like getModel() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
		}
64
		return $this;
65
	}
66
67
}
68