Completed
Push — master ( b7cc2d...e2c111 )
by Peter
14:18 queued 10:45
created

RelatedAnnotation::_getMeta()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.0237

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 31
ccs 21
cts 23
cp 0.913
rs 8.439
cc 6
eloc 16
nc 24
nop 0
crap 6.0237
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 http://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
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
35
 */
36
class RelatedAnnotation extends ManganPropertyAnnotation
37
{
38
39
	public $class;
40
	public $join;
41
	public $updatable;
42
	public $value;
43
44 1
	public function init()
45
	{
46 1
		$relMeta = $this->_getMeta();
47 1
		$relMeta->single = true;
48 1
		$relMeta->isArray = false;
49 1
		$this->_entity->related = $relMeta;
50 1
		$this->_entity->propagateEvents = true;
51 1
		$this->_entity->owned = true;
52 1
		$this->_entity->decorators[] = RelatedDecorator::class;
53 1
		$this->_entity->decorators[] = EmbedRefDecorator::class;
54 1
	}
55
56
	/**
57
	 *
58
	 * @return RelatedMeta
59
	 */
60 3
	protected function _getMeta()
61
	{
62 3
		$data = ParamsExpander::expand($this, ['class', 'join', 'sort', 'updatable']);
63 3
		if (empty($this->_entity->related))
64 3
		{
65 3
			$relMeta = new RelatedMeta();
66 3
		}
67
		else
68
		{
69
			$relMeta = $this->_entity->related;
70
		}
71 3
		foreach ($data as $key => $val)
72
		{
73 3
			$relMeta->$key = $val;
74 3
		}
75 3
		if (!$relMeta->class)
76 3
		{
77 1
			$relMeta->class = $this->_meta->type()->name;
78 1
		}
79 3
		if (empty($relMeta->join))
80 3
		{
81
			throw new UnexpectedValueException(sprintf('Parameter `join` is required for `%s`, model `%s`, field `%s`', static::class, $this->_meta->type()->name, $this->_entity->name));
82
		}
83 3
		if (empty($relMeta->sort))
84 3
		{
85 3
			$relMeta->sort = [
1 ignored issue
show
Documentation Bug introduced by
It seems like array('_id' => \Maslosof...SortInterface::SortAsc) of type array<string,?> is incompatible with the declared type array<integer,integer> of property $sort.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
				'_id' => SortInterface::SortAsc
87 3
			];
88 3
		}
89 3
		return $relMeta;
90
	}
91
92
}
93