Optimized ParameterManagement

This commit is contained in:
Kevin Frantz 2019-03-28 22:17:59 +01:00
parent 481cc327cd
commit d2baca0822
8 changed files with 66 additions and 12 deletions

View File

@ -9,7 +9,6 @@ use Infinito\Exception\NotDefinedException;
/** /**
* @todo Rename class! * @todo Rename class!
* deprecated use ValidGetParameterService instead
* *
* @author kevinfrantz * @author kevinfrantz
*/ */

View File

@ -10,36 +10,50 @@ namespace Infinito\Domain\ParameterManagement;
interface OptionalGetParameterServiceInterface interface OptionalGetParameterServiceInterface
{ {
/** /**
* @deprecated
*
* @var string * @var string
*/ */
const VERSION_PARAMETER = 'version'; const VERSION_PARAMETER = 'version';
/** /**
* @deprecated
*
* @var string * @var string
*/ */
const EXECUTE_PARAMETER = 'execute'; const EXECUTE_PARAMETER = 'execute';
/** /**
* @deprecated
*
* @var string * @var string
*/ */
const VIEW_PARAMETER = 'view'; const VIEW_PARAMETER = 'view';
/** /**
* @deprecated
*
* @var string * @var string
*/ */
const CLASS_PARAMETER = 'class'; const CLASS_PARAMETER = 'class';
/** /**
* @deprecated
*
* @var string * @var string
*/ */
const FRAME_PARAMETER = 'frame'; const FRAME_PARAMETER = 'frame';
/** /**
* @deprecated
*
* @var string * @var string
*/ */
const SCHEMA_PARAMETER = 'schema'; const SCHEMA_PARAMETER = 'schema';
/** /**
* @deprecated
*
* @var array|string[] * @var array|string[]
*/ */
const OPTIONAL_PARAMETERS = [ const OPTIONAL_PARAMETERS = [

View File

@ -3,15 +3,45 @@
namespace Infinito\Domain\ParameterManagement\Parameter; namespace Infinito\Domain\ParameterManagement\Parameter;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use Infinito\Exception\SetNotPossibleException;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
final class FrameParameter extends AbstractParameter final class FrameParameter extends AbstractParameter
{ {
/**
* @var bool The standart value which will be used
*/
const STANDART_VALUE = true;
/** /**
* @var int|null * @var int|null
* @Assert\GreaterThan(0) * @Assert\Type("bool")
*/ */
protected $value; protected $value;
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\ParameterManagement\Parameter\AbstractParameter::setValue()
*/
public function setValue($value): void
{
if (is_null($value)) {
//Use standart value
$this->value = self::STANDART_VALUE;
return;
}
if (is_numeric($value)) {
$number = (int) $value;
if ($number >= 0 && $number <= 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!');
}
} }

View File

@ -6,7 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use HaydenPierce\ClassFinder\ClassFinder; use HaydenPierce\ClassFinder\ClassFinder;
use Infinito\Domain\ParameterManagement\Parameter\ParameterInterface; use Infinito\Domain\ParameterManagement\Parameter\ParameterInterface;
use Infinito\Exception\NoValidChoiceException; use Infinito\Exception\NotDefinedException;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -67,7 +67,7 @@ final class ParameterFactory implements ParameterFactoryInterface
if ($parameter) { if ($parameter) {
return $parameter; return $parameter;
} }
throw new NoValidChoiceException("The parameter for key <<$key>> doesn't exist!"); throw new NotDefinedException("The parameter for key <<$key>> doesn't exist! Generate a parameter class in the parameter folder!");
} }
/** /**

View File

@ -44,8 +44,8 @@ final class ValidGetParametersService extends OptionalGetParameterService implem
$parameter = $this->parameterFactory->getParameter($key); $parameter = $this->parameterFactory->getParameter($key);
$parameter->setValue($this->currentRequest->get($key)); $parameter->setValue($this->currentRequest->get($key));
$errors = $this->validator->validate($parameter); $errors = $this->validator->validate($parameter);
if (count($errors) > 0) { foreach ($errors as $error) {
throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation"); throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation; Message: <<".$error->getMessage().'>> ,Value: <<'.$parameter->getValue().'>> .');
} }
} }
} }

View File

@ -47,7 +47,7 @@ final class ViewBuilder implements ViewBuilderInterface
/** /**
* @var ValidGetParameterServiceInterface * @var ValidGetParameterServiceInterface
*/ */
private $optionalGetParameterService; private $validGetParameterService;
/** /**
* Containes the routine to decide if the template should be loaded with or without frame. * Containes the routine to decide if the template should be loaded with or without frame.
@ -56,8 +56,8 @@ final class ViewBuilder implements ViewBuilderInterface
*/ */
private function checkLoadWithFrame(): bool private function checkLoadWithFrame(): bool
{ {
if ($this->optionalGetParameterService->hasParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER)) { if ($this->validGetParameterService->hasParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER)) {
return $this->optionalGetParameterService->getParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER); return $this->validGetParameterService->getParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER);
} }
return true; return true;
@ -91,12 +91,12 @@ final class ViewBuilder implements ViewBuilderInterface
* @param ActionServiceInterface $actionService * @param ActionServiceInterface $actionService
* @param ActionFactoryServiceInterface $actionFactoryService * @param ActionFactoryServiceInterface $actionFactoryService
*/ */
public function __construct(ActionServiceInterface $actionService, ActionFactoryServiceInterface $actionFactoryService, TemplateNameServiceInterface $templateNameService, ValidGetParameterServiceInterface $optionalGetParameterService) public function __construct(ActionServiceInterface $actionService, ActionFactoryServiceInterface $actionFactoryService, TemplateNameServiceInterface $templateNameService, ValidGetParameterServiceInterface $validGetParameterService)
{ {
$this->view = View::create(); $this->view = View::create();
$this->actionService = $actionService; $this->actionService = $actionService;
$this->templateNameService = $templateNameService; $this->templateNameService = $templateNameService;
$this->optionalGetParameterService = $optionalGetParameterService; $this->validGetParameterService = $validGetParameterService;
} }
/** /**

View File

@ -40,6 +40,6 @@ class FrameFunctionTest extends WebTestCase
{ {
$client = static::createClient(); $client = static::createClient();
$client->request(Request::METHOD_GET, 'api/rest/source/HOMEPAGE.html?frame=true'); $client->request(Request::METHOD_GET, 'api/rest/source/HOMEPAGE.html?frame=true');
$this->assertEquals(406, $client->getResponse()->getStatusCode()); $this->assertEquals(500, $client->getResponse()->getStatusCode());
} }
} }

View File

@ -5,6 +5,7 @@ namespace tests\Unit\Domain\ParameterManagement;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Infinito\Domain\ParameterManagement\ParameterFactory; use Infinito\Domain\ParameterManagement\ParameterFactory;
use Infinito\Domain\ParameterManagement\Parameter\VersionParameter; use Infinito\Domain\ParameterManagement\Parameter\VersionParameter;
use Infinito\Exception\NotDefinedException;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -19,4 +20,14 @@ class ParameterFactoryTest extends TestCase
$this->assertInstanceOf(VersionParameter::class, $versionParameter); $this->assertInstanceOf(VersionParameter::class, $versionParameter);
$this->assertEquals($versionParameter, $parameterFactory->getParameter('version')); $this->assertEquals($versionParameter, $parameterFactory->getParameter('version'));
} }
public function testGetParameter(): void
{
$parameterFactory = new ParameterFactory();
$versionParameter = $parameterFactory->getParameter('version');
$this->assertInstanceOf(VersionParameter::class, $versionParameter);
$this->assertEquals($versionParameter, $parameterFactory->getParameter('version'));
$this->expectException(NotDefinedException::class);
$versionParameter = $parameterFactory->getParameter('blabalbal');
}
} }