Completed
Push — master ( f32624...4e9dd5 )
by Peter
05:55
created

AbstractScopeManager::defaultScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Maslosoft\Mangan\Abstracts;
4
5
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface;
6
7
/**
8
 * Base class for implementing scope managers
9
 *
10
 * @see ScopeManagerInterface
11
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
12
 */
13
abstract class AbstractScopeManager
14
{
15
16
	/**
17
	 * Model instance
18
	 * @var AnnotatedInterface
19
	 */
20
	private $model = null;
21
22
	/**
23
	 *
24
	 * @var CriteriaInterface
25
	 */
26
	private $criteria = null;
27
28
	/**
29
	 * Returns the declaration of named scopes.
30
	 * A named scope represents a query criteria that can be chained together with
31
	 * other named scopes and applied to a query. This method should be overridden
32
	 * by child classes to declare named scopes for the particular document classes.
33
	 * For example, the following code declares two named scopes: 'recently' and
34
	 * 'published'.
35
	 * <pre>
36
	 * return array(
37
	 * 	'published'=>array(
38
	 * 		'conditions'=>array(
39
	 * 				'status'=>array('==', 1),
40
	 * 		),
41
	 * 	),
42
	 * 	'recently'=>array(
43
	 * 		'sort'=>array('create_time'=>Criteria::SortDesc),
44
	 * 		'limit'=>5,
45
	 * 	),
46
	 * );
47
	 * </pre>
48
	 * If the above scopes are declared in a 'Post' model, we can perform the following
49
	 * queries:
50
	 * <pre>
51
	 * $posts=Post::model()->published()->findAll();
52
	 * $posts=Post::model()->published()->recently()->findAll();
53
	 * $posts=Post::model()->published()->published()->recently()->find();
54
	 * </pre>
55
	 *
56
	 * @return array the scope definition. The array keys are scope names; the array
57
	 * values are the corresponding scope definitions. Each scope definition is represented
58
	 * as an array whose keys must be properties of {@link Criteria}.
59
	 * @since v1.0
60
	 */
61
	public function scopes()
62
	{
63
		return [];
64
	}
65
66
	/**
67
	 * Returns the default named scope that should be implicitly applied to all queries for this model.
68
	 * Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries.
69
	 * The default implementation simply returns an empty array. You may override this method
70
	 * if the model needs to be queried with some default criteria (e.g. only active records should be returned).
71
	 * @return array the mongo criteria. This will be used as the parameter to the constructor
72
	 * of {@link Criteria}.
73
	 * @since v1.2.2
74
	 */
75
	public function defaultScope()
76
	{
77
		return [];
78
	}
79
80
	/**
81
	 * Resets all scopes and criteria applied including default scope.
82
	 *
83
	 * @return Document
84
	 * @since v1.0
85
	 */
86
	public function resetScope()
87
	{
88
		$this->criteria = $this->getNewCriteria();
89
		return $this;
90
	}
91
92
	/**
93
	 * Apply scopes to criteria, will create criteria object if not provided and pass it by reference
94
	 * @param CriteriaInterface|array|null $criteria
95
	 * @return CriteriaInterface
96
	 */
97 86
	public function apply(&$criteria = null)
98
	{
99 86
		if (null === $criteria)
100
		{
101 24
			return $this->getModelCriteria();
102
		}
103 81
		elseif (is_array($criteria))
104
		{
105
			$criteria = $this->getNewCriteria($criteria);
106
		}
107 81
		$criteria->mergeWith($this->criteria);
108 81
		$criteria->mergeWith($this->getModelCriteria());
109 81
		return $criteria;
110
	}
111
112
	public function reset()
113
	{
114
		$this->criteria = $this->getNewCriteria();
115
		return $this;
116
	}
117
118 86
	protected function getModelCriteria()
119
	{
120 86
		$criteria = null;
121 86
		if ($this->model instanceof WithCriteriaInterface)
0 ignored issues
show
Bug introduced by
The class Maslosoft\Mangan\Abstracts\WithCriteriaInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
122
		{
123
			$criteria = $this->model->getDbCriteria();
124
		}
125 86
		elseif ($this->model instanceof CriteriaAwareInterface)
0 ignored issues
show
Bug introduced by
The class Maslosoft\Mangan\Abstracts\CriteriaAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
126
		{
127
			$criteria = $this->model->getCriteria();
128
		}
129 86
		if (empty($criteria))
130
		{
131 86
			return $this->getNewCriteria();
132
		}
133
		return $criteria;
134
	}
135
136
	abstract protected function getNewCriteria($criteria = null);
137
}
138