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

AnnotationAggregateIdExtractor::extract()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.2
cc 4
eloc 14
nc 6
nop 1
crap 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