Completed
Push — master ( 8afce9...d9c6a8 )
by Peter
27:47 queued 23:46
created

DbRefAnnotation::getDbRefMeta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 2
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\DbRefDecorator;
18
use Maslosoft\Mangan\Decorators\EmbedRefDecorator;
19
use Maslosoft\Mangan\Meta\DbRefMeta;
20
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
21
22
/**
23
 * DB reference annotation
24
 *
25
 * Most simple usage:
26
 * ```
27
 * @DbRef(Vendor\Package\ClassLiteral)
28
 * ```
29
 *
30
 * Disable updates, long notation:
31
 * ```
32
 * @DbRef(Vendor\Package\ClassLiteral, updatable = false)
33
 * ```
34
 *
35
 * Disable updates, short notation:
36
 * ```
37
 * @DbRef(Vendor\Package\ClassLiteral, false)
38
 * ```
39
 *
40
 * @template DbRef(${class}, ${updatable})
41
 *
42
 * @Conflicts('Embedded')
43
 * @Conflicts('EmbeddedArray')
44
 * @Conflicts('DbRefArray')
45
 * @Conflicts('Related')
46
 * @Conflicts('RelatedArray')
47
 *
48
 * @Target('property')
49
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
50
 */
51
class DbRefAnnotation extends ManganPropertyAnnotation
52
{
53
54
	public $class;
55
	public $updatable;
56
	public $value;
57
58 4
	public function init()
59
	{
60 4
		$refMeta = $this->getDbRefMeta();
61 4
		$refMeta->single = true;
62 4
		$refMeta->isArray = false;
63 4
		$this->getEntity()->dbRef = $refMeta;
64 4
		$this->getEntity()->propagateEvents = true;
65 4
		$this->getEntity()->owned = true;
66 4
		$this->getEntity()->decorators[] = DbRefDecorator::class;
67 4
		$this->getEntity()->decorators[] = EmbedRefDecorator::class;
68 4
	}
69
70 8
	protected function getDbRefMeta()
71
	{
72 8
		$data = ParamsExpander::expand($this, ['class', 'updatable']);
73 8
		$refMeta = new DbRefMeta($data);
74 8
		if (!$refMeta->class)
75 8
		{
76 1
			$refMeta->class = $this->getMeta()->type()->name;
77 1
		}
78 8
		$this->getEntity()->updatable = $refMeta->updatable;
79 8
		return $refMeta;
80
	}
81
82
}
83