SearchTypeAnnotation::init()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.2098
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Annotations;
14
15
use Maslosoft\Addendum\Utilities\ClassChecker;
16
use Maslosoft\Manganel\Meta\ManganelTypeAnnotation;
17
use UnexpectedValueException;
18
19
/**
20
 * Search Type Annotation
21
 *
22
 * Use this annotation to override searched type. This can be used
23
 * to allow search based on hierarchy of models. So that partial
24
 * models could be passed as parameter to query builder and it
25
 * will search for proper type.
26
 *
27
 * Type should be class name, or dot notation name.
28
 *
29
 * Example usage:
30
 * ```
31
 * @SearchType(MyVendror\MyPackage\MyDerivedClass)
32
 * ```
33
 *
34
 * @Target('class')
35
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
36
 */
37
class SearchTypeAnnotation extends ManganelTypeAnnotation
38
{
39
40
	const Ns = __NAMESPACE__;
41
42
	/**
43
	 * Document type.
44
	 * @var string
45
	 */
46
	public $value = null;
47
48 1
	public function init()
49
	{
50 1
		if (empty($this->value))
51
		{
52
			throw new UnexpectedValueException(sprintf('@SearchType annotation requires type name as param, used on model `%s`', $this->getMeta()->type()->name));
53
		}
54 1
		if (!ClassChecker::exists($this->value))
55
		{
56
			throw new UnexpectedValueException(sprintf('Could not resolve class name for @SearchType annotation used on model `%s`', $this->getMeta()->type()->name));
57
		}
58 1
		$this->getEntity()->type = $this->value;
59 1
	}
60
61
}
62