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

ChainAggregateIdExtractor::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace DDDominio\EventSourcing\Common;
4
5
class ChainAggregateIdExtractor implements AggregateIdExtractorInterface
6
{
7
    /**
8
     * @var AggregateIdExtractorInterface[]
9
     */
10
    private $extractors = [];
11
12
    /**
13
     * @param AggregateIdExtractorInterface $aggregateIdExtractor
14
     */
15 4
    public function add(AggregateIdExtractorInterface $aggregateIdExtractor)
16
    {
17 4
        $this->extractors[] = $aggregateIdExtractor;
18 4
    }
19
20
    /**
21
     * @param object $aggregate
22
     * @return string
23
     */
24 5
    public function extract($aggregate)
25
    {
26 5
        foreach ($this->extractors as $extractor) {
27
            try {
28 4
                return $extractor->extract($aggregate);
29 2
            } catch (AggregateIdNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
30
            }
31
        }
32 2
        throw new AggregateIdNotFoundException(sprintf('Id cannot be extracted from %s', get_class($aggregate)));
33
    }
34
}
35