Passed
Push — master ( dba989...50b90e )
by Mr
06:45
created

EntityListTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
dl 0
loc 35
ccs 10
cts 16
cp 0.625
rs 10
c 3
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 15 3
A diff() 0 13 4
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/entity 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\Entity;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\ValueObject\ValueObjectListTrait;
13
14
trait EntityListTrait
15
{
16
    use ValueObjectListTrait;
17
18
    /** @param null|array $state */
19 10
    public static function fromNative($state): self
20
    {
21 10
        Assertion::nullOrIsTraversable($state, "State provided to '".static::class."' must be null or iterable.");
22
23 10
        $entities = [];
24 10
        $typeFactories = static::getTypeFactories();
25 10
        if (!is_null($state)) {
26 10
            foreach ($state as $data) {
27 10
                Assertion::keyExists($data, EntityInterface::TYPE_KEY, 'Entity state is missing type key.');
28 10
                $entityType = $data[EntityInterface::TYPE_KEY];
29 10
                $entities[] = $typeFactories[$entityType]($data);
30
            }
31
        }
32
33 10
        return new static($entities);
34
    }
35
36
    public function diff(EntityListInterface $list): self
37
    {
38
        $differingEntities = [];
39
        foreach ($this as $pos => $entity) {
40
            /**
41
             * @psalm-suppress PossiblyNullArgument
42
             * @psalm-suppress ArgumentTypeCoercion
43
             */
44
            if (!$list->has($pos) || !(new EntityDiff)($entity, $list->get($pos))->isEmpty()) {
45
                $differingEntities[] = $entity;
46
            }
47
        }
48
        return new static($differingEntities);
49
    }
50
}
51