Optimized Parameter logic

This commit is contained in:
Kevin Frantz 2019-03-28 19:54:57 +01:00
parent 65e2fc03d0
commit 341ff5d5df
4 changed files with 37 additions and 8 deletions

View File

@ -24,7 +24,7 @@ class OptionalGetParameterService implements OptionalGetParameterServiceInterfac
*
* @throws UnvalidParameterException If the parameter is not valid
*/
private function validateParameter(string $key): void
protected function validateParameter(string $key): void
{
if (in_array($key, self::OPTIONAL_PARAMETERS)) {
return;

View File

@ -5,6 +5,9 @@ namespace Infinito\Domain\ParameterManagement;
use Infinito\Domain\ParameterManagement\Parameter\ParameterInterface;
use Doctrine\Common\Collections\Collection;
/**
* @author kevinfrantz
*/
interface ParameterFactoryInterface
{
/**

View File

@ -7,10 +7,4 @@ namespace Infinito\Domain\ParameterManagement;
*/
interface ValidGetParameterServiceInterface extends OptionalGetParameterServiceInterface
{
/**
* @param string $key
*
* @return bool checks if the parameter is valid
*/
public function isValid(string $key): bool;
}

View File

@ -2,12 +2,44 @@
namespace Infinito\Domain\ParameterManagement;
use Infinito\Exception\UnvalidParameterException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @author kevinfrantz
*/
final class ValidGetParametersService extends OptionalGetParameterService implements ValidGetParameterServiceInterface
{
public function isValid(string $key): bool
/**
* @var ParameterFactoryInterface
*/
private $parameterFactory;
/**
* @var ValidatorInterface
*/
private $validator;
public function __construct(RequestStack $requestStack, ParameterFactoryInterface $parameterFactory, ValidatorInterface $validator)
{
$this->parameterFactory = $parameterFactory;
$this->validator = $validator;
parent::__construct($requestStack);
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\ParameterManagement\OptionalGetParameterService::validateParameter()
*/
protected function validateParameter(string $key): void
{
parent::validateParameter($key);
$parameter = $this->parameterFactory->getParameter($key);
$errors = $this->validator->validate($parameter);
if (count($errors) > 0) {
throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation");
}
}
}