Passed
Push — master ( a714a7...2bb7f4 )
by Thorsten
02:02
created

EntityDiff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 6
dl 0
loc 34
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 5 1
A assertComparabiliy() 0 6 2
B generateDiff() 0 18 5
1
<?php
2
3
namespace Daikon\Entity\Entity;
4
5
use Daikon\Entity\EntityType\AttributeInterface;
6
use Daikon\Entity\Exception\UnexpectedType;
7
8
final class EntityDiff
9
{
10 4
    public function __invoke(EntityInterface $leftEntity, EntityInterface $rightEntity): ValueObjectMap
11
    {
12 4
        $this->assertComparabiliy($leftEntity, $rightEntity);
13 4
        return $this->generateDiff($leftEntity, $rightEntity);
14
    }
15
16 4
    private function assertComparabiliy(EntityInterface $leftEntity, EntityInterface $rightEntity)
17
    {
18 4
        if ($leftEntity->getEntityType() !== $rightEntity->getEntityType()) {
19
            throw new UnexpectedType('Comparing entities of different types is not supported.');
20
        }
21 4
    }
22
23 4
    private function generateDiff(EntityInterface $leftEntity, EntityInterface $rightEntity): ValueObjectMap
24
    {
25 4
        return ValueObjectMap::forEntity($leftEntity, array_reduce(
26 4
            $leftEntity->getEntityType()->getAttributes()->toArray(),
27 4
            function (array $diff, AttributeInterface $attribute) use ($leftEntity, $rightEntity): array {
28 4
                $attrName = $attribute->getName();
29 4
                if ($rightEntity->has($attrName) && $leftEntity->has($attrName)) {
30 4
                    if (!$leftEntity->get($attrName)->equals($rightEntity->get($attrName))) {
31 3
                        $diff[$attrName] = $leftEntity->get($attrName);
32
                    }
33 1
                } elseif ($leftEntity->has($attrName)) {
34 1
                    $diff[$attrName] = $leftEntity->get($attrName);
35
                }
36 4
                return $diff;
37 4
            },
38 4
            []
39
        ));
40
    }
41
}
42