Skip to content

Commit 9e47bef

Browse files
committed
Splitted the logic into smaller methods
1 parent d41d388 commit 9e47bef

1 file changed

Lines changed: 48 additions & 41 deletions

File tree

ScriptHandler.php

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,10 @@ public static function buildParameters(Event $event)
3939

4040
private static function processFile(array $config, IOInterface $io)
4141
{
42-
if (empty($config['file'])) {
43-
throw new \InvalidArgumentException('The extra.incenteev-parameters.file setting is required to use this script handler.');
44-
}
42+
$config = self::processConfig($config);
4543

4644
$realFile = $config['file'];
47-
48-
if (empty($config['dist-file'])) {
49-
$distFile = $realFile.'.dist';
50-
} else {
51-
$distFile = $config['dist-file'];
52-
}
53-
54-
$keepOutdatedParams = false;
55-
if (isset($config['keep-outdated'])) {
56-
$keepOutdatedParams = (boolean) $config['keep-outdated'];
57-
}
58-
59-
if (empty($config['parameter-key'])) {
60-
$parameterKey = 'parameters';
61-
} else {
62-
$parameterKey = $config['parameter-key'];
63-
}
64-
65-
if (!is_file($distFile)) {
66-
throw new \InvalidArgumentException(sprintf('The dist file "%s" does not exist. Check your dist-file config or create it.', $distFile));
67-
}
45+
$parameterKey = $config['parameter-key'];
6846

6947
$exists = is_file($realFile);
7048

@@ -74,7 +52,7 @@ private static function processFile(array $config, IOInterface $io)
7452
$io->write(sprintf('<info>%s the "%s" file.</info>', $action, $realFile));
7553

7654
// Find the expected params
77-
$expectedValues = $yamlParser->parse(file_get_contents($distFile));
55+
$expectedValues = $yamlParser->parse(file_get_contents($config['dist-file']));
7856
if (!isset($expectedValues[$parameterKey])) {
7957
throw new \InvalidArgumentException('The dist file seems invalid.');
8058
}
@@ -89,12 +67,55 @@ private static function processFile(array $config, IOInterface $io)
8967
}
9068
$actualValues = array_merge($actualValues, $existingValues);
9169
}
92-
$actualParams = (array) $actualValues[$parameterKey];
9370

71+
$actualValues[$parameterKey] = self::processParams($config, $io, $expectedParams, (array) $actualValues[$parameterKey]);
72+
73+
// Preserve other top-level keys than `$parameterKey` in the file
74+
foreach ($expectedValues as $key => $setting) {
75+
if (!array_key_exists($key, $actualValues)) {
76+
$actualValues[$key] = $setting;
77+
}
78+
}
79+
80+
if (!is_dir($dir = dirname($realFile))) {
81+
mkdir($dir, 0755, true);
82+
}
83+
84+
file_put_contents($realFile, "# This file is auto-generated during the composer install\n" . Yaml::dump($actualValues, 99));
85+
}
86+
87+
private static function processConfig(array $config)
88+
{
89+
if (empty($config['file'])) {
90+
throw new \InvalidArgumentException('The extra.incenteev-parameters.file setting is required to use this script handler.');
91+
}
92+
93+
if (empty($config['dist-file'])) {
94+
$config['dist-file'] = $config['file'].'.dist';
95+
}
96+
97+
if (!is_file($config['dist-file'])) {
98+
throw new \InvalidArgumentException(sprintf('The dist file "%s" does not exist. Check your dist-file config or create it.', $config['dist-file']));
99+
}
100+
101+
if (empty($config['parameter-key'])) {
102+
$config['parameter-key'] = 'parameters';
103+
}
104+
105+
return $config;
106+
}
107+
108+
private static function processParams(array $config, IOInterface $io, $expectedParams, $actualParams)
109+
{
94110
// Grab values for parameters that were renamed
95111
$renameMap = empty($config['rename-map']) ? array() : (array) $config['rename-map'];
96112
$actualParams = array_replace($actualParams, self::getRenameValues($renameMap, $actualParams));
97113

114+
$keepOutdatedParams = false;
115+
if (isset($config['keep-outdated'])) {
116+
$keepOutdatedParams = (boolean) $config['keep-outdated'];
117+
}
118+
98119
if (!$keepOutdatedParams) {
99120
// Remove the outdated params
100121
foreach ($actualParams as $key => $value) {
@@ -109,21 +130,7 @@ private static function processFile(array $config, IOInterface $io)
109130
// Add the params coming from the environment values
110131
$actualParams = array_replace($actualParams, self::getEnvValues($envMap));
111132

112-
$actualParams = self::getParams($io, $expectedParams, $actualParams);
113-
114-
// Preserve other top-level keys than `$parameterKey` in the file
115-
$actualValues[$parameterKey] = $actualParams;
116-
foreach ($expectedValues as $key => $setting) {
117-
if (!array_key_exists($key, $actualValues)) {
118-
$actualValues[$key] = $setting;
119-
}
120-
}
121-
122-
if (!is_dir($dir = dirname($realFile))) {
123-
mkdir($dir, 0755, true);
124-
}
125-
126-
file_put_contents($realFile, "# This file is auto-generated during the composer install\n" . Yaml::dump($actualValues, 99));
133+
return self::getParams($io, $expectedParams, $actualParams);
127134
}
128135

129136
private static function getEnvValues(array $envMap)

0 commit comments

Comments
 (0)