Skip to content

Commit

Permalink
Add more env default values
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Dec 21, 2022
1 parent 100cb13 commit a4b39b9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ You can run movary commands in docker via e.g. `docker exec movary php bin/conso

It is recommended to enable tmdb image caching (set env variable `TMDB_ENABLE_IMAGE_CACHING=1`).

##### Available environment variables with defaults:
##### Available environment variables with their default values:

```
### Enviroment
Expand All @@ -153,7 +153,7 @@ MIN_RUNTIME_IN_SECONDS_FOR_JOB_PROCESSING=15
DATABASE_MODE=
DATABASE_SQLITE=storage/movary.sqlite
DATABASE_MYSQL_HOST=
DATABASE_MYSQL_PORT=
DATABASE_MYSQL_PORT=3306
DATABASE_MYSQL_NAME=
DATABASE_MYSQL_USER=
DATABASE_MYSQL_PASSWORD=
Expand Down
7 changes: 3 additions & 4 deletions docker-compose.override.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ services:
- "8080:80"
environment:
PHP_DATE_TIMEZONE: "${TIMEZONE}"
TMDB_API_KEY: "${TMDB_API_KEY}"
TMDB_ENABLE_IMAGE_CACHING: 1
DATABASE_MODE: "mysql"
DATABASE_MYSQL_HOST: "mysql"
DATABASE_MYSQL_PORT: "${DATABASE_MYSQL_PORT}"
DATABASE_MYSQL_NAME: "${DATABASE_MYSQL_NAME}"
DATABASE_MYSQL_USER: "${DATABASE_MYSQL_USER}"
DATABASE_MYSQL_PASSWORD: "${DATABASE_MYSQL_PASSWORD}"
DATABASE_MYSQL_NAME: "${DATABASE_MYSQL_NAME}"
TMDB_API_KEY: "${TMDB_API_KEY}"
TMDB_ENABLE_IMAGE_CACHING: 1
volumes:
- movary-storage:/app/storage

Expand Down
4 changes: 2 additions & 2 deletions settings/phinx.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
$databaseConfig = [
'adapter' => 'mysql',
'host' => $config->getAsString('DATABASE_MYSQL_HOST'),
'port' => $config->getAsString('DATABASE_MYSQL_PORT'),
'port' => \Movary\Factory::getDatabaseMysqlPort($config),
'name' => $config->getAsString('DATABASE_MYSQL_NAME'),
'user' => $config->getAsString('DATABASE_MYSQL_USER'),
'pass' => $config->getAsString('DATABASE_MYSQL_PASSWORD'),
'charset' => $config->getAsString('DATABASE_MYSQL_CHARSET'),
'charset' => \Movary\Factory::getDatabaseMysqlCharset($config),
'collation' => 'utf8_unicode_ci',
];
} else {
Expand Down
38 changes: 21 additions & 17 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@

class Factory
{
private const DEFAULT_MIN_RUNTIME_IN_SECONDS_FOR_JOB_PROCESSING = 15;

private const DEFAULT_DATABASE_MYSQL_CHARSET = 'utf8mb4';

private const DEFAULT_DATABASE_MYSQL_PORT = 3306;

private const DEFAULT_LOG_LEVEL = LogLevel::WARNING;

private const DEFAULT_APPLICATION_VERSION = null;
Expand Down Expand Up @@ -99,11 +105,11 @@ public static function createDbConnection(Config $config) : DBAL\Connection
'mysql' => [
'driver' => 'pdo_mysql',
'host' => $config->getAsString('DATABASE_MYSQL_HOST'),
'port' => $config->getAsInt('DATABASE_MYSQL_PORT'),
'port' => self::getDatabaseMysqlPort($config),
'dbname' => $config->getAsString('DATABASE_MYSQL_NAME'),
'user' => $config->getAsString('DATABASE_MYSQL_USER'),
'password' => $config->getAsString('DATABASE_MYSQL_PASSWORD'),
'charset' => $config->getAsString('DATABASE_MYSQL_CHARSET'),
'charset' => self::getDatabaseMysqlCharset($config),
],
default => throw new \RuntimeException('Not supported database mode: ' . $databaseMode)
};
Expand Down Expand Up @@ -258,6 +264,16 @@ public static function getDatabaseMode(Config $config) : string
return $config->getAsString('DATABASE_MODE');
}

public static function getDatabaseMysqlCharset(mixed $config) : string
{
return $config->getAsString('DATABASE_MYSQL_CHARSET', self::DEFAULT_DATABASE_MYSQL_CHARSET);
}

public static function getDatabaseMysqlPort(Config $config) : int
{
return $config->getAsInt('DATABASE_MYSQL_PORT', self::DEFAULT_DATABASE_MYSQL_PORT);
}

private static function createLoggerStreamHandlerFile(ContainerInterface $container, Config $config) : StreamHandler
{
$streamHandler = new StreamHandler(
Expand All @@ -279,29 +295,17 @@ private static function createLoggerStreamHandlerStdout(ContainerInterface $cont

private static function getLogLevel(Config $config) : string
{
try {
return $config->getAsString('LOG_LEVEL');
} catch (OutOfBoundsException) {
return self::DEFAULT_LOG_LEVEL;
}
return $config->getAsString('LOG_LEVEL', self::DEFAULT_LOG_LEVEL);
}

private static function getTmdbEnabledImageCaching(Config $config) : bool
{
try {
return $config->getAsBool('TMDB_ENABLE_IMAGE_CACHING');
} catch (OutOfBoundsException) {
return self::DEFAULT_TMDB_IMAGE_CACHING;
}
return $config->getAsBool('TMDB_ENABLE_IMAGE_CACHING', self::DEFAULT_TMDB_IMAGE_CACHING);
}

public function createProcessJobCommand(ContainerInterface $container, Config $config) : Command\ProcessJobs
{
try {
$minRuntimeInSeconds = $config->getAsInt('MIN_RUNTIME_IN_SECONDS_FOR_JOB_PROCESSING');
} catch (OutOfBoundsException) {
$minRuntimeInSeconds = null;
}
$minRuntimeInSeconds = $config->getAsInt('MIN_RUNTIME_IN_SECONDS_FOR_JOB_PROCESSING', self::DEFAULT_MIN_RUNTIME_IN_SECONDS_FOR_JOB_PROCESSING);

return new Command\ProcessJobs(
$container->get(JobQueueApi::class),
Expand Down
46 changes: 30 additions & 16 deletions src/ValueObject/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,51 @@ public function __construct(private readonly array $config)
{
}

public static function createFromEnv() : self
public static function createFromEnv(array $additionalData = []) : self
{
$fpmEnvironment = $_ENV;
$systemEnvironment = getenv();

return new self(array_merge($fpmEnvironment, $systemEnvironment));
return new self(array_merge($fpmEnvironment, $systemEnvironment, $additionalData));
}

public function getAsArray(string $parameter) : array
public function getAsBool(string $parameter, ?bool $fallbackValue = null) : bool
{
return (array)$this->get($parameter);
}
try {
return (bool)$this->get($parameter);
} catch (OutOfBoundsException $e) {
if ($fallbackValue === null) {
throw $e;
}

public function getAsBool(string $parameter) : bool
{
return (bool)$this->get($parameter);
return $fallbackValue;
}
}

public function getAsFloat(string $parameter) : float
public function getAsInt(string $parameter, ?int $fallbackValue = null) : int
{
return (float)$this->get($parameter);
}
try {
return (int)$this->get($parameter);
} catch (OutOfBoundsException $e) {
if ($fallbackValue === null) {
throw $e;
}

public function getAsInt(string $parameter) : int
{
return (int)$this->get($parameter);
return $fallbackValue;
}
}

public function getAsString(string $parameter) : string
public function getAsString(string $parameter, ?string $fallbackValue = null) : string
{
return (string)$this->get($parameter);
try {
return (string)$this->get($parameter);
} catch (OutOfBoundsException $e) {
if ($fallbackValue === null) {
throw $e;
}

return $fallbackValue;
}
}

private function ensureKeyExists(string $key) : void
Expand Down
53 changes: 53 additions & 0 deletions tests/unit/ValueObject/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Tests\Unit\Movary\ValueObject;

use Movary\ValueObject\Config;
use PHPUnit\Framework\TestCase;

/** @covers \Movary\ValueObject\Config */
class ConfigTest extends TestCase
{
private Config $subject;

public function setUp() : void
{
$this->subject = Config::createFromEnv(
[
'string_test' => 'value',
'int_test' => 2,
'bool_test' => true,
],
);
}

public function testGetAsBool() : void
{
self::assertSame(true, $this->subject->getAsBool('bool_test'));
self::assertSame(false, $this->subject->getAsBool('bool_test_not_existing', false));

$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Key does not exist: bool_test_not_existing');
$this->subject->getAsBool('bool_test_not_existing');
}

public function testGetAsInt() : void
{
self::assertSame(2, $this->subject->getAsInt('int_test'));
self::assertSame(3, $this->subject->getAsInt('int_test_not_existing', 3));

$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Key does not exist: int_test_not_existing');
$this->subject->getAsBool('int_test_not_existing');
}

public function testGetAsString() : void
{
self::assertSame('value', $this->subject->getAsString('string_test'));
self::assertSame('fallback', $this->subject->getAsString('string_test_not_existing', 'fallback'));

$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Key does not exist: string_test_not_existing');
$this->subject->getAsBool('string_test_not_existing');
}
}

0 comments on commit a4b39b9

Please sign in to comment.