Completed
Push — master ( b6905f...386860 )
by Peter
07:45
created

ScopeAnnotation   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 4
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 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\Annotations;
15
16
use function is_a;
17
use Maslosoft\Addendum\Helpers\ParamsExpander;
18
use Maslosoft\Addendum\Utilities\ClassChecker;
19
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
20
use Maslosoft\Mangan\Interfaces\ScopeInterface;
21
use Maslosoft\Mangan\Meta\ManganTypeAnnotation;
22
23
/**
24
 * Annotation can be used to add extra scope
25
 * for model. Scopes will add extra criteria when
26
 * finding, updating and removing model.
27
 *
28
 * The scope is a simple class implementing `ScopeInterface`.
29
 * The `ScopeInterface` method `getCriteria` must return `CriteriaInterface`,
30
 * most likely `Criteria` instance.
31
 *
32
 * Usage:
33
 *
34
 * ```
35
 * @Scope(EmbeddedClassName)
36
 * ```
37
 *
38
 * @Target('class')
39
 * @template Scope(${defaultClassName})
40
 * @see ScopeInterface
41
 * @see CriteriaInterface
42
 * @author Piotr
43
 */
44
class ScopeAnnotation extends ManganTypeAnnotation
45
{
46
47
	public $value = true;
48
49 2
	public function init()
50
	{
51 2
		$data = ParamsExpander::expand($this, ['class']);
52 2
		$className = $data['class'];
53 2
		assert(ClassChecker::exists($className));
54 2
		assert(is_a($className, ScopeInterface::class, true));
55 2
		$this->getEntity()->scopes[] = $className;
56 2
	}
57
58
}
59