1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\ComposerConfigManager\Application\Updater; |
3
|
|
|
|
4
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Author; |
5
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Autoload; |
6
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Configuration; |
7
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationItem; |
8
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Package; |
9
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Script; |
10
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\SuggestedPackage; |
11
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Support; |
12
|
|
|
|
13
|
|
|
class ConfigurationUpdater |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param Configuration $baseConfiguration |
17
|
|
|
* @param Configuration $newConfiguration |
18
|
|
|
* |
19
|
|
|
* @return Configuration |
20
|
|
|
*/ |
21
|
|
|
public function update(Configuration $baseConfiguration, Configuration $newConfiguration) |
22
|
|
|
{ |
23
|
|
|
return new Configuration( |
24
|
|
|
$this->updateIfDefined($newConfiguration->getPackageName(), $baseConfiguration->getPackageName()), |
25
|
|
|
$this->updateIfDefined($newConfiguration->getType(), $baseConfiguration->getType()), |
26
|
|
|
$this->updateIfDefined($newConfiguration->getLicense(), $baseConfiguration->getLicense()), |
27
|
|
|
$this->updateIfDefined($newConfiguration->getPackageVersion(), $baseConfiguration->getPackageVersion()), |
28
|
|
|
$this->updateIfDefined($newConfiguration->getDescription(), $baseConfiguration->getDescription()), |
29
|
|
|
$this->mergeKeywordList($newConfiguration->getKeywordList(), $baseConfiguration->getKeywordList()), |
30
|
|
|
$this->updateList($newConfiguration->getAuthorList(), $baseConfiguration->getAuthorList()), |
31
|
|
|
$this->updateList( |
32
|
|
|
$newConfiguration->getProvidedPackageList(), |
33
|
|
|
$baseConfiguration->getProvidedPackageList() |
34
|
|
|
), |
35
|
|
|
$this->updateList( |
36
|
|
|
$newConfiguration->getSuggestedPackageList(), |
37
|
|
|
$baseConfiguration->getSuggestedPackageList() |
38
|
|
|
), |
39
|
|
|
$this->updateList($newConfiguration->getSupportList(), $baseConfiguration->getSupportList()), |
40
|
|
|
$this->updateList($newConfiguration->getAutoloadList(), $baseConfiguration->getAutoloadList()), |
41
|
|
|
$this->updateList($newConfiguration->getAutoloadDevList(), $baseConfiguration->getAutoloadDevList()), |
42
|
|
|
$this->updateList( |
43
|
|
|
$newConfiguration->getRequiredPackageList(), |
44
|
|
|
$baseConfiguration->getRequiredPackageList() |
45
|
|
|
), |
46
|
|
|
$this->updateList( |
47
|
|
|
$newConfiguration->getRequiredDevPackageList(), |
48
|
|
|
$baseConfiguration->getRequiredDevPackageList() |
49
|
|
|
), |
50
|
|
|
$this->updateList($newConfiguration->getScriptList(), $baseConfiguration->getScriptList()) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $baseValue |
56
|
|
|
* @param string $newValue |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function updateIfDefined($newValue, $baseValue) |
61
|
|
|
{ |
62
|
|
|
return $newValue ? $newValue : $baseValue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string[] $newList |
67
|
|
|
* @param string[] $oldList |
68
|
|
|
* |
69
|
|
|
* @return string[] |
70
|
|
|
*/ |
71
|
|
|
protected function mergeKeywordList(array $oldList, array $newList) |
72
|
|
|
{ |
73
|
|
|
return array_values( |
74
|
|
|
array_unique( |
75
|
|
|
array_merge($newList, $oldList) |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ConfigurationItem[] $newEntityList |
82
|
|
|
* @param ConfigurationItem[] $oldEntityList |
83
|
|
|
* |
84
|
|
|
* @return ConfigurationItem[] |
85
|
|
|
*/ |
86
|
|
|
protected function updateList(array $newEntityList, array $oldEntityList) |
87
|
|
|
{ |
88
|
|
|
$existingEntityIdList = []; |
89
|
|
|
foreach ($newEntityList as $entity) { |
90
|
|
|
$existingEntityIdList[$entity->getItemId()] = true; |
91
|
|
|
} |
92
|
|
|
$normalizedOldEntityList = []; |
93
|
|
|
foreach ($oldEntityList as $oldEntity) { |
94
|
|
|
if (!array_key_exists($oldEntity->getItemId(), $existingEntityIdList)) { |
95
|
|
|
$normalizedOldEntityList[] = $oldEntity; |
96
|
|
|
} else { |
97
|
|
|
// A new entity have been defined, loop over new entity list and append all entities with the same id |
98
|
|
|
$oldEntityId = $oldEntity->getItemId(); |
99
|
|
|
foreach ($newEntityList as $newEntityKey => $newEntity) { |
100
|
|
|
if ($newEntity->getItemId() == $oldEntityId) { |
101
|
|
|
$normalizedOldEntityList[] = $this->mergeEntity($oldEntity, $newEntity); |
102
|
|
|
unset($newEntityList[$newEntityKey]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return array_merge($normalizedOldEntityList, $newEntityList); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param ConfigurationItem $oldEntity |
113
|
|
|
* @param ConfigurationItem $newEntity |
114
|
|
|
* |
115
|
|
|
* @return ConfigurationItem |
116
|
|
|
*/ |
117
|
|
|
protected function mergeEntity($oldEntity, $newEntity) |
118
|
|
|
{ |
119
|
|
|
switch (true) { |
120
|
|
|
case $newEntity instanceof Author && $oldEntity instanceof Author: |
121
|
|
|
return new Author( |
122
|
|
|
$newEntity->getName(), |
123
|
|
|
$newEntity->getEmail() ? $newEntity->getEmail() : $oldEntity->getEmail(), |
124
|
|
|
$newEntity->getRole() ? $newEntity->getRole() : $oldEntity->getRole() |
125
|
|
|
); |
126
|
|
|
break; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $newEntity; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.