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

AggregateAnnotated   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 33
rs 10
ccs 14
cts 15
cp 0.9333
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAnnotation() 0 14 4
A getAnnotatedId() 0 3 1
A getAnnotatedRevision() 0 3 1
A getAggregateId() 0 3 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