Completed
Push — master ( 898d30...0483c4 )
by Peter
21:25
created

RelatedAnnotation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 1
A _getMeta() 0 10 2
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\Annotations;
10
11
use Maslosoft\Addendum\Helpers\ParamsExpander;
12
use Maslosoft\Mangan\Decorators\DbRefDecorator;
13
use Maslosoft\Mangan\Decorators\EmbedRefDecorator;
14
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
15
use Maslosoft\Mangan\Meta\RelatedMeta;
16
17
/**
18
 * RelatedAnnotation
19
 * Shorthand notation:
20
 *
21
 * Related(Company\Project\Projects, '_id' => 'entity_id', true)
22
 *
23
 * Expanded notation:
24
 *
25
 * Related(class = Company\Project\Projects, join = {'_id' => 'entity_id'}, updatable = true)
26
 *
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
class RelatedAnnotation extends ManganPropertyAnnotation
30
{
31
32
	public $class;
33
	public $join;
34
	public $updatable;
35
	public $value;
36
37
	public function init()
38
	{
39
		$relMeta = $this->_getMeta();
40
		$relMeta->single = true;
41
		$relMeta->isArray = false;
42
		$this->_entity->related = $relMeta;
43
		$this->_entity->propagateEvents = true;
44
		$this->_entity->owned = true;
45
		$this->_entity->decorators[] = DbRefDecorator::class;
46
		$this->_entity->decorators[] = EmbedRefDecorator::class;
47
	}
48
49
	/**
50
	 *
51
	 * @return RelatedMeta
52
	 */
53
	protected function _getMeta()
54
	{
55
		$data = ParamsExpander::expand($this, ['class', 'join', 'updatable']);
56
		$relMeta = new RelatedMeta($data);
57
		if (!$relMeta->class)
58
		{
59
			$relMeta->class = $this->_meta->type()->name;
60
		}
61
		return $relMeta;
62
	}
63
64
}
65