Skip to content

Commit

Permalink
Merge pull request #26 from seregazhuk/config-code-improvement
Browse files Browse the repository at this point in the history
Some code improvements for config Builder
  • Loading branch information
seregazhuk committed Oct 12, 2019
2 parents 7fb04f7 + 4269724 commit 1cfc782
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Config/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Builder

public function build(InputInterface $input): Config
{
$valuesFromFile = $this->valuesFromConfigFile($this->getConfigPath($input));
$valuesFromFile = $this->getValuesFromConfigFile($input);
$commandLineValues = $this->valuesFromCommandLineArgs($input);
$configValues = array_replace_recursive($valuesFromFile, $commandLineValues);

Expand All @@ -34,7 +34,12 @@ public function build(InputInterface $input): Config

private function valuesFromConfigFile(string $configFilePath): array
{
$values = Yaml::parse(file_get_contents($configFilePath));
$contents = file_get_contents($configFilePath);
if ($contents === false) {
throw InvalidConfigFileContents::invalidContents($configFilePath);
}

$values = Yaml::parse($contents);
if ($values === null) {
throw InvalidConfigFileContents::invalidContents($configFilePath);
}
Expand Down Expand Up @@ -69,9 +74,21 @@ private function valuesFromCommandLineArgs(InputInterface $input): array
];
}

private function getConfigPath(InputInterface $input): string
private function getConfigPath(InputInterface $input): ?string
{
$pathFromCommandLine = $input->getOption('config');
$pathFromCommandLine = (string)$input->getOption('config');

return empty($pathFromCommandLine) ? $this->findConfigFile() : $pathFromCommandLine;
}

private function getValuesFromConfigFile(InputInterface $input): array
{
$configFilePath = $this->getConfigPath($input);
if ($configFilePath === null) {
$valuesFromFile = [];
} else {
$valuesFromFile = $this->valuesFromConfigFile($configFilePath);
}
return $valuesFromFile;
}
}

0 comments on commit 1cfc782

Please sign in to comment.