Test Failed
Pull Request — master (#10)
by Yo
02:26
created

ListUpdater   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B update() 0 24 6
1
<?php
2
namespace Yoanm\ComposerConfigManager\Application\Updater;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationItemInterface;
5
6
class ListUpdater
7
{
8
    /**
9
     * @param ConfigurationItemInterface[] $newEntityList
10
     * @param ConfigurationItemInterface[] $oldEntityList
11
     *
12
     * @return ConfigurationItemInterface[]
13
     */
14
    public function update(array $newEntityList, array $oldEntityList)
15
    {
16
        $existingEntityIdList = [];
17
        foreach ($newEntityList as $entity) {
18
            $existingEntityIdList[$entity->getItemId()] = true;
19
        }
20
        $normalizedOldEntityList = [];
21
        foreach ($oldEntityList as $oldEntity) {
22
            if (!array_key_exists($oldEntity->getItemId(), $existingEntityIdList)) {
23
                $normalizedOldEntityList[] = $oldEntity;
24
            } else {
25
                // A new entity have been defined, loop over new entity list and append all entities with the same id
26
                $oldEntityId = $oldEntity->getItemId();
27
                foreach ($newEntityList as $newEntityKey => $newEntity) {
28
                    if ($newEntity->getItemId() == $oldEntityId) {
29
                        $normalizedOldEntityList[] = $newEntity;
30
                        unset($newEntityList[$newEntityKey]);
31
                    }
32
                }
33
            }
34
        }
35
36
        return array_merge($normalizedOldEntityList, $newEntityList);
37
    }
38
}
39