1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\ComposerConfigManager\Application\Updater; |
3
|
|
|
|
4
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Author; |
5
|
|
|
|
6
|
|
|
class AuthorListUpdater |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @param Author[] $newEntityList |
10
|
|
|
* @param Author[] $oldEntityList |
11
|
|
|
* |
12
|
|
|
* @return Author[] |
13
|
|
|
*/ |
14
|
3 |
|
public function update(array $newEntityList, array $oldEntityList) |
15
|
|
|
{ |
16
|
3 |
|
$existingEntityIdList = []; |
17
|
3 |
|
foreach ($newEntityList as $entity) { |
18
|
3 |
|
$existingEntityIdList[$entity->getItemId()] = true; |
19
|
3 |
|
} |
20
|
3 |
|
$normalizedOldEntityList = []; |
21
|
3 |
|
foreach ($oldEntityList as $oldEntity) { |
22
|
2 |
|
if (!array_key_exists($oldEntity->getItemId(), $existingEntityIdList)) { |
23
|
2 |
|
$normalizedOldEntityList[] = $oldEntity; |
24
|
2 |
|
} else { |
25
|
|
|
// A new entity have been defined, loop over new entity list and append all entities with the same id |
26
|
1 |
|
$oldEntityId = $oldEntity->getItemId(); |
27
|
1 |
|
foreach ($newEntityList as $newEntityKey => $newEntity) { |
28
|
1 |
|
if ($newEntity->getItemId() == $oldEntityId) { |
29
|
1 |
|
$normalizedOldEntityList[] = $this->mergeAuthor($newEntity, $oldEntity); |
30
|
1 |
|
unset($newEntityList[$newEntityKey]); |
31
|
1 |
|
} |
32
|
1 |
|
} |
33
|
|
|
} |
34
|
3 |
|
} |
35
|
|
|
|
36
|
3 |
|
return array_merge($normalizedOldEntityList, $newEntityList); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Author $newEntity |
41
|
|
|
* @param Author $oldEntity |
42
|
|
|
* @return Author |
43
|
|
|
*/ |
44
|
1 |
|
protected function mergeAuthor(Author $newEntity, Author $oldEntity) |
45
|
|
|
{ |
46
|
1 |
|
return new Author( |
47
|
1 |
|
$newEntity->getName(), |
48
|
1 |
|
$newEntity->getEmail() ? $newEntity->getEmail() : $oldEntity->getEmail(), |
49
|
1 |
|
$newEntity->getRole() ? $newEntity->getRole() : $oldEntity->getRole() |
50
|
1 |
|
); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|