Optimized ParameterManagement

This commit is contained in:
Kevin Frantz
2019-03-28 23:51:24 +01:00
parent d2baca0822
commit 62f953c706
10 changed files with 111 additions and 93 deletions

View File

@@ -22,14 +22,12 @@ class OptionalGetParameterService implements OptionalGetParameterServiceInterfac
/**
* @param string $key
*
* @deprecated
*
* @throws UnvalidParameterException If the parameter is not valid
*/
protected function validateParameter(string $key): void
{
if (in_array($key, self::OPTIONAL_PARAMETERS)) {
return;
}
throw new UnvalidParameterException("Parameter <<$key>> isn't valid.");
}
/**

View File

@@ -9,13 +9,6 @@ namespace Infinito\Domain\ParameterManagement;
*/
interface OptionalGetParameterServiceInterface
{
/**
* @deprecated
*
* @var string
*/
const VERSION_PARAMETER = 'version';
/**
* @deprecated
*
@@ -57,7 +50,6 @@ interface OptionalGetParameterServiceInterface
* @var array|string[]
*/
const OPTIONAL_PARAMETERS = [
self::VERSION_PARAMETER,
self::VIEW_PARAMETER,
self::CLASS_PARAMETER,
self::FRAME_PARAMETER,

View File

@@ -3,6 +3,8 @@
namespace Infinito\Domain\ParameterManagement\Parameter;
/**
* Parameter classes shouldn't throw exceptions!
*
* @author kevinfrantz
*/
abstract class AbstractParameter implements ParameterInterface

View File

@@ -3,7 +3,7 @@
namespace Infinito\Domain\ParameterManagement\Parameter;
use Symfony\Component\Validator\Constraints as Assert;
use Infinito\Exception\SetNotPossibleException;
use Infinito\Exception\UnvalidGetParameterException;
/**
* @author kevinfrantz
@@ -16,7 +16,7 @@ final class FrameParameter extends AbstractParameter
const STANDART_VALUE = true;
/**
* @var int|null
* @var bool
* @Assert\Type("bool")
*/
protected $value;
@@ -28,20 +28,26 @@ final class FrameParameter extends AbstractParameter
*/
public function setValue($value): void
{
if (is_null($value)) {
//Use standart value
$this->value = self::STANDART_VALUE;
$type = gettype($value);
switch ($type) {
case 'NULL':
// Use standart value
$this->value = self::STANDART_VALUE;
return;
return;
case 'boolean':
$this->value = $value;
return;
}
if (is_numeric($value)) {
$number = (int) $value;
if ($number >= 0 && $number <= 1) {
$value = (int) $value;
if ($value >= 0 && $value <= 1) {
$this->value = (bool) $value;
return;
}
}
throw new SetNotPossibleException("It\'s not possible to set <<$value>> of type <<".gettype($value).'>> for class <<'.get_class().'>>. Just 0 and 1 are allowed!');
throw new UnvalidGetParameterException("It\'s not possible to set <<$value>> of type <<".$type.'>> for class <<'.get_class().'>>. Just 0 and 1 are allowed!');
}
}

View File

@@ -2,9 +2,9 @@
namespace Infinito\Domain\ParameterManagement;
use Infinito\Exception\UnvalidParameterException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Infinito\Exception\UnvalidGetParameterException;
/**
* @author kevinfrantz
@@ -40,12 +40,11 @@ final class ValidGetParametersService extends OptionalGetParameterService implem
*/
protected function validateParameter(string $key): void
{
parent::validateParameter($key);
$parameter = $this->parameterFactory->getParameter($key);
$parameter->setValue($this->currentRequest->get($key));
$errors = $this->validator->validate($parameter);
foreach ($errors as $error) {
throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation; Message: <<".$error->getMessage().'>> ,Value: <<'.$parameter->getValue().'>> .');
throw new UnvalidGetParameterException("Parameter <<$key>> didn't pass the validation; Message: <<".$error->getMessage().'>> ,Value: <<'.$parameter->getValue().'>> .');
}
}
}

View File

@@ -8,6 +8,7 @@ use Infinito\Domain\ActionManagement\ActionServiceInterface;
use Infinito\Domain\ActionManagement\ActionFactoryServiceInterface;
use Infinito\Domain\TemplateManagement\TemplateNameServiceInterface;
use Infinito\Domain\ParameterManagement\ValidGetParameterServiceInterface;
use Infinito\Domain\ParameterManagement\Parameter\FrameParameter;
/**
* @author kevinfrantz
@@ -56,8 +57,8 @@ final class ViewBuilder implements ViewBuilderInterface
*/
private function checkLoadWithFrame(): bool
{
if ($this->validGetParameterService->hasParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER)) {
return $this->validGetParameterService->getParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER);
if ($this->validGetParameterService->hasParameter(FrameParameter::getKey())) {
return $this->validGetParameterService->getParameter(FrameParameter::getKey());
}
return true;

View File

@@ -0,0 +1,12 @@
<?php
namespace Infinito\Exception;
/**
* Thrown when an unvalid get paramter is used.
*
* @author kevinfrantz
*/
class UnvalidGetParameterException extends UnvalidParameterException
{
}