Completed
Push — master ( a5631d...69c428 )
by Daniel
03:14
created

AnnotationAggregateIdExtractor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A extract() 0 20 4
1
<?php
2
3
namespace DDDominio\EventSourcing\Common;
4
5
use DDDominio\EventSourcing\Common\Annotation\AggregateId;
6
use Doctrine\Common\Annotations\AnnotationReader;
7
8
class AnnotationAggregateIdExtractor implements AggregateIdExtractorInterface
9
{
10
    /**
11
     * @param object $aggregate
12
     * @return string
13
     */
14 3
    public function extract($aggregate)
15
    {
16 3
        $reflection = new \ReflectionClass($aggregate);
17 3
        $annotationReader = new AnnotationReader();
18 3
        $aggregateIdMethodName = null;
19 3
        foreach ($reflection->getMethods() as $reflectionMethod) {
20 2
            $annotation = $annotationReader->getMethodAnnotation(
21
                $reflectionMethod,
22 2
                AggregateId::class
23
            );
24 2
            if (!is_null($annotation)) {
25 2
                $aggregateIdMethodName = $reflectionMethod->getName();
0 ignored issues
show
Bug introduced by
Consider using $reflectionMethod->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
26 2
                break;
27
            }
28
        }
29 3
        if (is_null($aggregateIdMethodName)) {
30 1
            throw new AggregateIdNotFoundException(sprintf('No "@AggregateId" annotation found in %s', get_class($aggregate)));
31
        }
32 2
        return (string) $aggregate->{$aggregateIdMethodName}();
33
    }
34
}
35