Passed
Push — master ( 134841...7712b0 )
by Mr
02:17
created

AggregateAnnotated::getAnnotatedRevision()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/event-sourcing project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\EventSourcing\Aggregate;
10
11
use Daikon\Interop\InheritanceReader;
12
use Daikon\Interop\InvalidArgumentException;
13
use ReflectionClass;
14
15
trait AggregateAnnotated
16
{
17
    use InheritanceReader;
18
19 4
    public function getAggregateId(): AggregateIdInterface
20
    {
21 4
        return $this->{static::getAnnotatedId()};
22
    }
23
24 4
    private static function getAnnotatedId(): string
25
    {
26 4
        return static::getAnnotation('id');
27
    }
28
29 4
    private static function getAnnotatedRevision(): string
30
    {
31 4
        return static::getAnnotation('rev');
32
    }
33
34 5
    private static function getAnnotation(string $key): string
35
    {
36 5
        $classReflection = new ReflectionClass(static::class);
37 5
        foreach (static::getInheritance($classReflection, true) as $curClass) {
38 5
            if (!($docComment = $curClass->getDocComment())) {
39 5
                continue;
40
            }
41 5
            preg_match("#@$key\((?<$key>\w+)#", $docComment, $matches);
42 5
            if (isset($matches[$key])) {
43 5
                return trim($matches[$key]);
44
            }
45
        }
46
47
        throw new InvalidArgumentException(sprintf("Missing @%s annotation on '%s'.", $key, static::class));
48
    }
49
}
50