Added a custom exception for invalid configuration options

This commit is contained in:
Chris Kankiewicz
2020-05-29 23:52:43 -07:00
parent 8e28d1fc44
commit c3232c6ac4
5 changed files with 36 additions and 12 deletions

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Exceptions;
use RuntimeException;
class InvalidConfiguration extends RuntimeException
{
/**
* Createn an exception from a configiraton option and value.
*
* @param string $option
* @param mixed $value
*
* @return self
*/
public static function fromConfig(string $option, $value): self
{
return new static(
sprintf("Unknown value %s for configuration option '%s'", var_export($value, true), $option)
);
}
}

View File

@@ -2,10 +2,10 @@
namespace App\Factories;
use App\Exceptions\InvalidConfiguration;
use Closure;
use DI\Container;
use PHLAK\Splat\Glob;
use RuntimeException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Tightenco\Collect\Support\Collection;
@@ -46,7 +46,7 @@ class FinderFactory
$finder->sort($sortOrder);
} else {
if (! array_key_exists($sortOrder, $this->container->get('sort_methods'))) {
throw new RuntimeException("Invalid sort option '{$sortOrder}'");
throw InvalidConfiguration::fromConfig('sort_order', $sortOrder);
}
$this->container->call($this->container->get('sort_methods')[$sortOrder], [$finder]);

View File

@@ -2,8 +2,8 @@
namespace App\Factories;
use App\Exceptions\InvalidConfiguration;
use DI\Container;
use RuntimeException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Translation\Loader\YamlFileLoader;
@@ -37,16 +37,17 @@ class TranslationFactory
*/
public function __invoke(): TranslatorInterface
{
$language = $this->container->get('language');
if (! in_array($language, $this->container->get('translations'))) {
throw new RuntimeException("Invalid language option '{$language}'");
if (! in_array(
$language = $this->container->get('language'),
$translations = $this->translations())
) {
throw InvalidConfiguration::fromConfig('language', $language);
}
$translator = new Translator($language);
$translator->addLoader('yaml', new YamlFileLoader());
foreach ($this->container->get('translations') as $language) {
foreach ($translations as $language) {
$translator->addResource('yaml', sprintf(
'%s/%s.yaml', $this->container->get('translations_path'), $language
), $language);

View File

@@ -2,8 +2,8 @@
namespace Tests\Factories;
use App\Exceptions\InvalidConfiguration;
use App\Factories\FinderFactory;
use RuntimeException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Tests\TestCase;
@@ -85,7 +85,7 @@ class FinderFactoryTest extends TestCase
{
$this->container->set('sort_order', 'invalid');
$this->expectException(RuntimeException::class);
$this->expectException(InvalidConfiguration::class);
(new FinderFactory($this->container, $this->cache))();
}

View File

@@ -2,8 +2,8 @@
namespace Tests\Factories;
use App\Exceptions\InvalidConfiguration;
use App\Factories\TranslationFactory;
use RuntimeException;
use Symfony\Component\Translation\MessageCatalogue;
use Tests\TestCase;
@@ -29,7 +29,7 @@ class TranslationFactoryTest extends TestCase
public function test_it_throws_an_exception_for_an_invalid_language(): void
{
$this->expectException(RuntimeException::class);
$this->expectException(InvalidConfiguration::class);
$this->container->set('language', 'xx');
(new TranslationFactory($this->container, $this->cache))();