Completed
Push — master ( 368ce8...5f1efa )
by Peter
05:51
created

RelatedAnnotation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 57
ccs 22
cts 24
cp 0.9167
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 1
B _getMeta() 0 31 6
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 Maslosoft\Addendum\Helpers\ParamsExpander;
17
use Maslosoft\Mangan\Decorators\EmbedRefDecorator;
18
use Maslosoft\Mangan\Decorators\RelatedDecorator;
19
use Maslosoft\Mangan\Interfaces\SortInterface;
20
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
21
use Maslosoft\Mangan\Meta\RelatedMeta;
22
use UnexpectedValueException;
23
24
/**
25
 * RelatedAnnotation
26
 * Shorthand notation:
27
 *
28
 * Related(Company\Project\Projects, join = {'_id' = 'entity_id'}, sort = {'_id' = 1}, true)
29
 *
30
 * Expanded notation:
31
 *
32
 * Related(class = Company\Project\Projects, join = {'_id' => 'entity_id'}, sort = {'_id' = 1}, updatable = true)
33
 *
34
 *
35
 * @Conflicts('Embedded')
36
 * @Conflicts('EmbeddedArray')
37
 * @Conflicts('DbRef')
38
 * @Conflicts('DbRefArray')
39
 * @Conflicts('RelatedArray')
40
 *
41
 *
42
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
43
 */
44
class RelatedAnnotation extends ManganPropertyAnnotation
45
{
46
47
	public $class;
48
	public $join;
49
	public $updatable;
50
	public $value;
51
52 1
	public function init()
53
	{
54 1
		$relMeta = $this->_getMeta();
55 1
		$relMeta->single = true;
56 1
		$relMeta->isArray = false;
57 1
		$this->getEntity()->related = $relMeta;
58 1
		$this->getEntity()->propagateEvents = true;
59 1
		$this->getEntity()->owned = true;
60 1
		$this->getEntity()->decorators[] = RelatedDecorator::class;
61 1
		$this->getEntity()->decorators[] = EmbedRefDecorator::class;
62 1
	}
63
64
	/**
65
	 *
66
	 * @return RelatedMeta
67
	 */
68 3
	protected function _getMeta()
69
	{
70 3
		$data = ParamsExpander::expand($this, ['class', 'join', 'sort', 'updatable']);
71 3
		if (empty($this->getEntity()->related))
72
		{
73 3
			$relMeta = new RelatedMeta();
74
		}
75
		else
76
		{
77
			$relMeta = $this->getEntity()->related;
78
		}
79 3
		foreach ($data as $key => $val)
80
		{
81 3
			$relMeta->$key = $val;
82
		}
83 3
		if (!$relMeta->class)
84
		{
85 1
			$relMeta->class = $this->getMeta()->type()->name;
86
		}
87 3
		if (empty($relMeta->join))
88
		{
89
			throw new UnexpectedValueException(sprintf('Parameter `join` is required for `%s`, model `%s`, field `%s`', static::class, $this->getMeta()->type()->name, $this->getEntity()->name));
90
		}
91 3
		if (empty($relMeta->sort))
92
		{
93 3
			$relMeta->sort = [
94
				'_id' => SortInterface::SortAsc
95
			];
96
		}
97 3
		return $relMeta;
98
	}
99
100
}
101