Used modern PHP features in CustomizableAppCOnfig

This commit is contained in:
Alejandro Celaya 2018-09-30 09:04:00 +02:00
parent ae9d99257e
commit 48f01921e1
2 changed files with 73 additions and 129 deletions

View File

@ -89,30 +89,44 @@
"@cs", "@cs",
"@stan", "@stan",
"@test", "@test",
"@func-test",
"@infect" "@infect"
], ],
"cs": "phpcs", "cs": "phpcs",
"cs-fix": "phpcbf", "cs:fix": "phpcbf",
"serve": "php -S 0.0.0.0:8000 -t public/", "stan": "phpstan analyse module/*/src/ --level=6 -c phpstan.neon",
"test": "phpunit --coverage-php build/coverage-unit.cov",
"pretty-test": "phpunit --coverage-html build/coverage", "test": [
"func-test": "phpunit -c phpunit-func.xml --coverage-php build/coverage-func.cov", "@test:unit",
"complete-pretty-test": [ "@test:func"
"@test", ],
"@func-test", "test:unit": "phpunit --coverage-php build/coverage-unit.cov",
"test:func": "phpunit -c phpunit-func.xml --coverage-php build/coverage-func.cov",
"test:pretty": [
"@test:unit",
"@test:func",
"phpcov merge build --html build/html" "phpcov merge build --html build/html"
], ],
"stan": "phpstan analyse module/*/src/ --level=6 -c phpstan.neon", "test:unit:pretty": "phpunit --coverage-html build/coverage",
"infect": "infection --threads=4 --min-msi=60 --only-covered --log-verbosity=2", "infect": "infection --threads=4 --min-msi=60 --only-covered --log-verbosity=2",
"infect-show": "infection --threads=4 --min-msi=60 --only-covered --log-verbosity=2 --show-mutations", "infect:show": "infection --threads=4 --min-msi=60 --only-covered --log-verbosity=2 --show-mutations"
"expressive": "expressive" },
"scripts-descriptions": {
"check": "<fg=blue;options=bold>Alias for \"cs\", \"stan\", \"test\" and \"infect\"</>",
"cs": "<fg=blue;options=bold>Checks coding styles</>",
"cs:fix": "<fg=blue;options=bold>Fixes coding styles, when possible</>",
"stan": "<fg=blue;options=bold>Inspects code with phpstan</>",
"test": "<fg=blue;options=bold>Runs all test suites</>",
"test:unit": "<fg=blue;options=bold>Runs unit test suites</>",
"test:func": "<fg=blue;options=bold>Runs functional test suites (covering entity repositories)</>",
"test:pretty": "<fg=blue;options=bold>Runs all test suites and generates an HTML code coverage report</>",
"test:unit:pretty": "<fg=blue;options=bold>Runs unit test suites and generates an HTML code coverage report</>",
"infect": "<fg=blue;options=bold>Checks unit tests quality applying mutation testing</>",
"infect:show": "<fg=blue;options=bold>Checks unit tests quality applying mutation testing and shows applied mutators</>"
}, },
"config": { "config": {
"process-timeout": 0, "sort-packages": true
"sort-packages": true,
"platform": {
"php": "7.1.8"
}
} }
} }

View File

@ -12,191 +12,126 @@ final class CustomizableAppConfig implements ArraySerializableInterface
/** /**
* @var array * @var array
*/ */
private $database; private $database = [];
/** /**
* @var array * @var array
*/ */
private $urlShortener; private $urlShortener = [];
/** /**
* @var array * @var array
*/ */
private $language; private $language = [];
/** /**
* @var array * @var array
*/ */
private $app; private $app = [];
/** /**
* @var string * @var string|null
*/ */
private $importedInstallationPath; private $importedInstallationPath;
/** public function getDatabase(): array
* @return array
*/
public function getDatabase()
{ {
return $this->database; return $this->database;
} }
/** public function setDatabase(array $database): self
* @param array $database
* @return $this
*/
public function setDatabase(array $database)
{ {
$this->database = $database; $this->database = $database;
return $this; return $this;
} }
/** public function hasDatabase(): bool
* @return bool
*/
public function hasDatabase()
{ {
return ! empty($this->database); return ! empty($this->database);
} }
/** public function getUrlShortener(): array
* @return array
*/
public function getUrlShortener()
{ {
return $this->urlShortener; return $this->urlShortener;
} }
/** public function setUrlShortener(array $urlShortener): self
* @param array $urlShortener
* @return $this
*/
public function setUrlShortener(array $urlShortener)
{ {
$this->urlShortener = $urlShortener; $this->urlShortener = $urlShortener;
return $this; return $this;
} }
/** public function hasUrlShortener(): bool
* @return bool
*/
public function hasUrlShortener()
{ {
return ! empty($this->urlShortener); return ! empty($this->urlShortener);
} }
/** public function getLanguage(): array
* @return array
*/
public function getLanguage()
{ {
return $this->language; return $this->language;
} }
/** public function setLanguage(array $language): self
* @param array $language
* @return $this
*/
public function setLanguage(array $language)
{ {
$this->language = $language; $this->language = $language;
return $this; return $this;
} }
/** public function hasLanguage(): bool
* @return bool
*/
public function hasLanguage()
{ {
return ! empty($this->language); return ! empty($this->language);
} }
/** public function getApp(): array
* @return array
*/
public function getApp()
{ {
return $this->app; return $this->app;
} }
/** public function setApp(array $app): self
* @param array $app
* @return $this
*/
public function setApp(array $app)
{ {
$this->app = $app; $this->app = $app;
return $this; return $this;
} }
/** public function hasApp(): bool
* @return bool
*/
public function hasApp()
{ {
return ! empty($this->app); return ! empty($this->app);
} }
/** public function getImportedInstallationPath(): ?string
* @return string
*/
public function getImportedInstallationPath()
{ {
return $this->importedInstallationPath; return $this->importedInstallationPath;
} }
/** public function setImportedInstallationPath(string $importedInstallationPath): self
* @param string $importedInstallationPath
* @return $this|self
*/
public function setImportedInstallationPath($importedInstallationPath)
{ {
$this->importedInstallationPath = $importedInstallationPath; $this->importedInstallationPath = $importedInstallationPath;
return $this; return $this;
} }
/** public function hasImportedInstallationPath(): bool
* @return bool
*/
public function hasImportedInstallationPath()
{ {
return $this->importedInstallationPath !== null; return $this->importedInstallationPath !== null;
} }
/** public function exchangeArray(array $array): void
* Exchange internal values from provided array
*
* @param array $array
* @return void
*/
public function exchangeArray(array $array)
{ {
if (isset($array['app_options'], $array['app_options']['secret_key'])) { $this->setApp([
$this->setApp([ 'SECRET' => $array['app_options']['secret_key'] ?? null,
'SECRET' => $array['app_options']['secret_key'], ]);
]);
}
if (isset($array['entity_manager'], $array['entity_manager']['connection'])) { $this->deserializeDatabase($array['entity_manager']['connection'] ?? []);
$this->deserializeDatabase($array['entity_manager']['connection']);
}
if (isset($array['translator'], $array['translator']['locale'], $array['cli'], $array['cli']['locale'])) { $this->setLanguage([
$this->setLanguage([ 'DEFAULT' => $array['translator']['locale'] ?? null,
'DEFAULT' => $array['translator']['locale'], 'CLI' => $array['cli']['locale'] ?? null,
'CLI' => $array['cli']['locale'], ]);
]);
}
if (isset($array['url_shortener'])) { $this->setUrlShortener([
$urlShortener = $array['url_shortener']; 'SCHEMA' => $array['url_shortener']['domain']['schema'] ?? null,
$this->setUrlShortener([ 'HOSTNAME' => $array['url_shortener']['domain']['hostname'] ?? null,
'SCHEMA' => $urlShortener['domain']['schema'], 'CHARS' => $array['url_shortener']['shortcode_chars'] ?? null,
'HOSTNAME' => $urlShortener['domain']['hostname'], 'VALIDATE_URL' => $array['url_shortener']['validate_url'] ?? true,
'CHARS' => $urlShortener['shortcode_chars'], ]);
'VALIDATE_URL' => $urlShortener['validate_url'] ?? true,
]);
}
} }
private function deserializeDatabase(array $conn) private function deserializeDatabase(array $conn): void
{ {
if (! isset($conn['driver'])) { if (! isset($conn['driver'])) {
return; return;
@ -205,22 +140,17 @@ final class CustomizableAppConfig implements ArraySerializableInterface
$params = ['DRIVER' => $driver]; $params = ['DRIVER' => $driver];
if ($driver !== 'pdo_sqlite') { if ($driver !== 'pdo_sqlite') {
$params['USER'] = $conn['user']; $params['USER'] = $conn['user'] ?? null;
$params['PASSWORD'] = $conn['password']; $params['PASSWORD'] = $conn['password'] ?? null;
$params['NAME'] = $conn['dbname']; $params['NAME'] = $conn['dbname'] ?? null;
$params['HOST'] = $conn['host']; $params['HOST'] = $conn['host'] ?? null;
$params['PORT'] = $conn['port']; $params['PORT'] = $conn['port'] ?? null;
} }
$this->setDatabase($params); $this->setDatabase($params);
} }
/** public function getArrayCopy(): array
* Return an array representation of the object
*
* @return array
*/
public function getArrayCopy()
{ {
$config = [ $config = [
'app_options' => [ 'app_options' => [