Created config post processor that parses a simplified config to what shlink expects

This commit is contained in:
Alejandro Celaya 2019-04-18 10:37:38 +02:00
parent e186237410
commit 479760c0ee
3 changed files with 146 additions and 1 deletions

View file

@ -25,4 +25,6 @@ return (new ConfigAggregator\ConfigAggregator([
env('APP_ENV') === 'test'
? new ConfigAggregator\PhpFileProvider('config/test/*.global.php')
: new ConfigAggregator\ArrayProvider([]),
], 'data/cache/app_config.php'))->getMergedConfig();
], 'data/cache/app_config.php', [
Core\ConfigPostProcessor::class,
]))->getMergedConfig();

View file

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core;
use Shlinkio\Shlink\Installer\Util\PathCollection;
use Zend\Stdlib\ArrayUtils;
use function array_intersect_key;
use function array_key_exists;
use function Functional\contains;
use function Functional\reduce_left;
class ConfigPostProcessor
{
private const SIMPLIFIED_CONFIG_MAPPING = [
'disable_track_param' => ['app_options', 'disable_track_param'],
'short_domain_schema' => ['url_shortener', 'domain', 'schema'],
'short_domain_host' => ['url_shortener', 'domain', 'hostname'],
'validate_url' => ['url_shortener', 'validate_url'],
'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'redirect_to'],
'db_config' => ['entity_manager', 'connection'],
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
'locale' => ['translator', 'locale'],
];
private const SIMPLIFIED_CONFIG_TOGGLES = [
'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'enable_redirection'],
'delete_short_url_threshold' => ['delete_short_urls', 'check_visits_threshold'],
];
private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config'];
public function __invoke(array $config): array
{
$existingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING);
return reduce_left($existingKeys, function ($value, string $key, $c, PathCollection $collection) {
$path = self::SIMPLIFIED_CONFIG_MAPPING[$key];
if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) {
$value = ArrayUtils::merge($collection->getValueInPath($path), $value);
}
$collection->setValueInPath($value, $path);
if (array_key_exists($key, self::SIMPLIFIED_CONFIG_TOGGLES)) {
$collection->setValueInPath(true, self::SIMPLIFIED_CONFIG_TOGGLES[$key]);
}
return $collection;
}, new PathCollection($config))->toArray();
}
}

View file

@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\ConfigPostProcessor;
use function array_merge;
class ConfigPostProcessorTest extends TestCase
{
private $postProcessor;
public function setUp(): void
{
$this->postProcessor = new ConfigPostProcessor();
}
/** @test */
public function properlyMapsSimplifiedConfig(): void
{
$config = [
'app_options' => [
'disable_track_param' => 'foo',
],
'entity_manager' => [
'connection' => [
'driver' => 'mysql',
'host' => 'shlink_db',
'port' => '3306',
],
],
];
$simplified = [
'disable_track_param' => 'bar',
'short_domain_schema' => 'https',
'short_domain_host' => 'doma.in',
'validate_url' => false,
'delete_short_url_threshold' => 50,
'locale' => 'es',
'not_found_redirect_to' => 'foobar.com',
'db_config' => [
'dbname' => 'shlink',
'user' => 'foo',
'password' => 'bar',
'port' => '1234',
],
];
$expected = [
'app_options' => [
'disable_track_param' => 'bar',
],
'entity_manager' => [
'connection' => [
'driver' => 'mysql',
'host' => 'shlink_db',
'dbname' => 'shlink',
'user' => 'foo',
'password' => 'bar',
'port' => '1234',
],
],
'url_shortener' => [
'domain' => [
'schema' => 'https',
'hostname' => 'doma.in',
],
'validate_url' => false,
'not_found_short_url' => [
'redirect_to' => 'foobar.com',
'enable_redirection' => true,
],
],
'translator' => [
'locale' => 'es',
],
'delete_short_urls' => [
'visits_threshold' => 50,
'check_visits_threshold' => true,
],
];
$result = ($this->postProcessor)(array_merge($config, $simplified));
$this->assertEquals(array_merge($expected, $simplified), $result);
}
}