mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-11-04 03:07:58 +00:00 
			
		
		
		
	Optimized Parameter logic
This commit is contained in:
		@@ -24,7 +24,7 @@ class OptionalGetParameterService implements OptionalGetParameterServiceInterfac
 | 
				
			|||||||
     *
 | 
					     *
 | 
				
			||||||
     * @throws UnvalidParameterException If the parameter is not valid
 | 
					     * @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)) {
 | 
					        if (in_array($key, self::OPTIONAL_PARAMETERS)) {
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,9 @@ namespace Infinito\Domain\ParameterManagement;
 | 
				
			|||||||
use Infinito\Domain\ParameterManagement\Parameter\ParameterInterface;
 | 
					use Infinito\Domain\ParameterManagement\Parameter\ParameterInterface;
 | 
				
			||||||
use Doctrine\Common\Collections\Collection;
 | 
					use Doctrine\Common\Collections\Collection;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author kevinfrantz
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
interface ParameterFactoryInterface
 | 
					interface ParameterFactoryInterface
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,10 +7,4 @@ namespace Infinito\Domain\ParameterManagement;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
interface ValidGetParameterServiceInterface extends OptionalGetParameterServiceInterface
 | 
					interface ValidGetParameterServiceInterface extends OptionalGetParameterServiceInterface
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * @param string $key
 | 
					 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @return bool checks if the parameter is valid
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function isValid(string $key): bool;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,12 +2,44 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace Infinito\Domain\ParameterManagement;
 | 
					namespace Infinito\Domain\ParameterManagement;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Infinito\Exception\UnvalidParameterException;
 | 
				
			||||||
 | 
					use Symfony\Component\HttpFoundation\RequestStack;
 | 
				
			||||||
 | 
					use Symfony\Component\Validator\Validator\ValidatorInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @author kevinfrantz
 | 
					 * @author kevinfrantz
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
final class ValidGetParametersService extends OptionalGetParameterService implements ValidGetParameterServiceInterface
 | 
					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");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user