mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2024-12-04 23:17:19 +01:00
Optimized ParameterManagement
This commit is contained in:
parent
481cc327cd
commit
d2baca0822
@ -9,7 +9,6 @@ use Infinito\Exception\NotDefinedException;
|
||||
|
||||
/**
|
||||
* @todo Rename class!
|
||||
* deprecated use ValidGetParameterService instead
|
||||
*
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
|
@ -10,36 +10,50 @@ namespace Infinito\Domain\ParameterManagement;
|
||||
interface OptionalGetParameterServiceInterface
|
||||
{
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const VERSION_PARAMETER = 'version';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const EXECUTE_PARAMETER = 'execute';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const VIEW_PARAMETER = 'view';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const CLASS_PARAMETER = 'class';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const FRAME_PARAMETER = 'frame';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SCHEMA_PARAMETER = 'schema';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @var array|string[]
|
||||
*/
|
||||
const OPTIONAL_PARAMETERS = [
|
||||
|
@ -3,15 +3,45 @@
|
||||
namespace Infinito\Domain\ParameterManagement\Parameter;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Infinito\Exception\SetNotPossibleException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
final class FrameParameter extends AbstractParameter
|
||||
{
|
||||
/**
|
||||
* @var bool The standart value which will be used
|
||||
*/
|
||||
const STANDART_VALUE = true;
|
||||
|
||||
/**
|
||||
* @var int|null
|
||||
* @Assert\GreaterThan(0)
|
||||
* @Assert\Type("bool")
|
||||
*/
|
||||
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!');
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use HaydenPierce\ClassFinder\ClassFinder;
|
||||
use Infinito\Domain\ParameterManagement\Parameter\ParameterInterface;
|
||||
use Infinito\Exception\NoValidChoiceException;
|
||||
use Infinito\Exception\NotDefinedException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
@ -67,7 +67,7 @@ final class ParameterFactory implements ParameterFactoryInterface
|
||||
if ($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!");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,8 +44,8 @@ final class ValidGetParametersService extends OptionalGetParameterService implem
|
||||
$parameter = $this->parameterFactory->getParameter($key);
|
||||
$parameter->setValue($this->currentRequest->get($key));
|
||||
$errors = $this->validator->validate($parameter);
|
||||
if (count($errors) > 0) {
|
||||
throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation");
|
||||
foreach ($errors as $error) {
|
||||
throw new UnvalidParameterException("Parameter <<$key>> didn't pass the validation; Message: <<".$error->getMessage().'>> ,Value: <<'.$parameter->getValue().'>> .');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ final class ViewBuilder implements ViewBuilderInterface
|
||||
/**
|
||||
* @var ValidGetParameterServiceInterface
|
||||
*/
|
||||
private $optionalGetParameterService;
|
||||
private $validGetParameterService;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
if ($this->optionalGetParameterService->hasParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER)) {
|
||||
return $this->optionalGetParameterService->getParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER);
|
||||
if ($this->validGetParameterService->hasParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER)) {
|
||||
return $this->validGetParameterService->getParameter(ValidGetParameterServiceInterface::FRAME_PARAMETER);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -91,12 +91,12 @@ final class ViewBuilder implements ViewBuilderInterface
|
||||
* @param ActionServiceInterface $actionService
|
||||
* @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->actionService = $actionService;
|
||||
$this->templateNameService = $templateNameService;
|
||||
$this->optionalGetParameterService = $optionalGetParameterService;
|
||||
$this->validGetParameterService = $validGetParameterService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,6 +40,6 @@ class FrameFunctionTest extends WebTestCase
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request(Request::METHOD_GET, 'api/rest/source/HOMEPAGE.html?frame=true');
|
||||
$this->assertEquals(406, $client->getResponse()->getStatusCode());
|
||||
$this->assertEquals(500, $client->getResponse()->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ namespace tests\Unit\Domain\ParameterManagement;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Infinito\Domain\ParameterManagement\ParameterFactory;
|
||||
use Infinito\Domain\ParameterManagement\Parameter\VersionParameter;
|
||||
use Infinito\Exception\NotDefinedException;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
@ -19,4 +20,14 @@ class ParameterFactoryTest extends TestCase
|
||||
$this->assertInstanceOf(VersionParameter::class, $versionParameter);
|
||||
$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');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user